ResourcesBrowser: implemented item-sensitive context menu

Implemented context menu for ResourcesBrowser with various actions
inside depending on selected item.

Signed-off-by: Tobias Doerffel <tobias.doerffel@gmail.com>
This commit is contained in:
Tobias Doerffel
2009-06-04 00:29:56 +02:00
parent c5bf76b98c
commit 359b73c2a3
5 changed files with 154 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

@@ -28,18 +28,43 @@
#include "side_bar_widget.h"
class QAction;
class ResourcesItem;
class ResourcesTreeModel;
class ResourcesTreeView;
class ResourcesBrowser : public sideBarWidget
{
Q_OBJECT
public:
enum Actions
{
EditProperties,
LoadProject,
LoadInNewTrackSongEditor,
LoadInNewTrackBBEditor,
LoadInActiveInstrumentTrack,
DownloadIntoCollection,
UploadToWWW,
DeleteLocalResource,
ImportFile,
NumActions
} ;
ResourcesBrowser( QWidget * _parent );
virtual ~ResourcesBrowser();
private slots:
void showContextMenu( const QPoint & _pos );
private:
void triggerAction( Actions _action, ResourcesItem * _item );
QAction * m_actions[NumActions];
ResourcesTreeModel * m_treeModel;
ResourcesTreeView * m_treeView;

View File

@@ -22,7 +22,9 @@
*
*/
#include <QtGui/QAction>
#include <QtGui/QLineEdit>
#include <QtGui/QMenu>
#include "resources_browser.h"
#include "resources_tree_model.h"
@@ -32,6 +34,37 @@
#include "embed.h"
struct ActionDesc
{
ResourcesBrowser::Actions action;
const char * pixmap;
const char * text;
} ;
static ActionDesc resourcesBrowserActions[] =
{
{ ResourcesBrowser::EditProperties, "edit_draw",
QT_TRANSLATE_NOOP( "ResourcesBrowser", "Show/edit properties" ) },
{ ResourcesBrowser::LoadProject, "project_open",
QT_TRANSLATE_NOOP( "ResourcesBrowser", "Load project" ) },
{ ResourcesBrowser::LoadInNewTrackSongEditor, "songeditor",
QT_TRANSLATE_NOOP( "ResourcesBrowser", "Load in new track in Song Editor" ) },
{ ResourcesBrowser::LoadInNewTrackBBEditor, "bb_track",
QT_TRANSLATE_NOOP( "ResourcesBrowser", "Load in new track in B+B Editor" ) },
{ ResourcesBrowser::LoadInActiveInstrumentTrack, "instrument_track",
QT_TRANSLATE_NOOP( "ResourcesBrowser", "Load into active instrument track" ) },
{ ResourcesBrowser::DownloadIntoCollection, "mimetypes/folder-downloads",
QT_TRANSLATE_NOOP( "ResourcesBrowser", "Download into collection" ) },
{ ResourcesBrowser::UploadToWWW, "mimetypes/network-workgroup",
QT_TRANSLATE_NOOP( "ResourcesBrowser", "Upload to WWW" ) },
{ ResourcesBrowser::DeleteLocalResource, "edit-delete",
QT_TRANSLATE_NOOP( "ResourcesBrowser", "Delete resource" ) },
{ ResourcesBrowser::ImportFile, "project_import",
QT_TRANSLATE_NOOP( "ResourcesBrowser", "Import file" ) }
} ;
ResourcesBrowser::ResourcesBrowser( QWidget * _parent ) :
sideBarWidget( tr( "Resources Browser" ),
@@ -47,6 +80,12 @@ ResourcesBrowser::ResourcesBrowser( QWidget * _parent ) :
QLineEdit * filterEdit = new QLineEdit ( contentParent() );
// set up context menu handling
m_treeView->setContextMenuPolicy( Qt::CustomContextMenu );
connect( m_treeView,
SIGNAL( customContextMenuRequested( const QPoint & ) ),
this, SLOT( showContextMenu( const QPoint & ) ) );
// add widgets to us (we're a SideBarWidget)
addContentWidget( m_treeView );
addContentWidget( filterEdit );
@@ -55,6 +94,19 @@ ResourcesBrowser::ResourcesBrowser( QWidget * _parent ) :
// instantly apply filter when typing into filterEdit
connect( filterEdit, SIGNAL( textChanged( const QString & ) ),
m_treeView, SLOT( setFilter( const QString & ) ) );
// setup actions to be used in context menu
for( int i = 0;i < (int) ( sizeof( resourcesBrowserActions ) /
sizeof( ActionDesc ) ); ++i )
{
Actions a = resourcesBrowserActions[i].action;
m_actions[a] = new QAction(
embed::getIconPixmap(
resourcesBrowserActions[i].pixmap ),
tr( resourcesBrowserActions[i].text ),
this );
m_actions[a]->setData( i );
}
}
@@ -68,5 +120,82 @@ ResourcesBrowser::~ResourcesBrowser()
void ResourcesBrowser::showContextMenu( const QPoint & _pos )
{
// clicked at a valid position?
QModelIndex idx = m_treeView->indexAt( _pos );
if( !idx.isValid() )
{
return;
}
// construct menu depending on selected item
QMenu m;
ResourcesItem * item = m_treeModel->item( idx );
switch( item->type() )
{
case ResourcesItem::TypeSample:
case ResourcesItem::TypeSoundFont:
case ResourcesItem::TypePreset:
case ResourcesItem::TypePlugin:
m.addAction( m_actions[LoadInNewTrackSongEditor] );
m.addAction( m_actions[LoadInNewTrackBBEditor] );
m.addAction( m_actions[LoadInActiveInstrumentTrack] );
break;
case ResourcesItem::TypeProject:
m.addAction( m_actions[LoadProject] );
break;
case ResourcesItem::TypeForeignProject:
case ResourcesItem::TypeMidiFile:
m.addAction( m_actions[ImportFile] );
break;
case ResourcesItem::TypeImage:
case ResourcesItem::TypeDirectory:
case ResourcesItem::TypeUnknown:
case ResourcesItem::NumTypes:
break;
}
if( item->type() != ResourcesItem::TypeDirectory )
{
m.addSeparator();
if( item->isLocalResource() )
{
m.addAction( m_actions[DeleteLocalResource] );
m.addAction( m_actions[UploadToWWW] );
}
else
{
m.addAction( m_actions[DownloadIntoCollection] );
}
}
m.addSeparator();
m.addAction( m_actions[EditProperties] );
// show and exec menu
QAction * a = m.exec( m_treeView->mapToGlobal( _pos ) );
if( a )
{
// trigger action if one has been selected
triggerAction( static_cast<Actions>( a->data().toInt() ),
item );
}
}
void ResourcesBrowser::triggerAction( Actions _action, ResourcesItem * _item )
{
// TODO
}
#include "moc_resources_browser.cxx"