From 582ce407de4ae17fc2936d547c765a3ef9e432c6 Mon Sep 17 00:00:00 2001 From: Tobias Doerffel Date: Mon, 26 Oct 2009 23:44:57 +0100 Subject: [PATCH] Added generic and flexible ResourceSelectDialog class Functionality from QuickLoadDialog which can be used in similiar dialogs for selecting resources has been moved into generic and highly flexible base class ResourceSelectDialog. --- include/QuickLoadDialog.h | 15 +--- include/ResourceSelectDialog.h | 73 ++++++++++++++++++ src/gui/QuickLoadDialog.cpp | 38 +++------ src/gui/ResourceSelectDialog.cpp | 128 +++++++++++++++++++++++++++++++ 4 files changed, 214 insertions(+), 40 deletions(-) create mode 100644 include/ResourceSelectDialog.h create mode 100644 src/gui/ResourceSelectDialog.cpp diff --git a/include/QuickLoadDialog.h b/include/QuickLoadDialog.h index e9abb6ee2..0e1cf3ee8 100644 --- a/include/QuickLoadDialog.h +++ b/include/QuickLoadDialog.h @@ -25,29 +25,22 @@ #ifndef _QUICK_LOAD_DIALOG_H #define _QUICK_LOAD_DIALOG_H -#include - #include "ResourceItem.h" +#include "ResourceSelectDialog.h" namespace Ui { class QuickLoadDialog; } -class ResourceListModel; -class QuickLoadDialog : public QDialog +class QuickLoadDialog : public ResourceSelectDialog { - Q_OBJECT public: QuickLoadDialog( QWidget * _parent, ResourceItem::Type _typeFilter = - ResourceItem::TypeUnknown ); + ResourceItem::TypeUnknown, + DatabaseScope _databaseScope = AllResources ); virtual ~QuickLoadDialog(); -private slots: - void setTypeFilter( int ); - - private: Ui::QuickLoadDialog * ui; - ResourceListModel * m_listModel; } ; diff --git a/include/ResourceSelectDialog.h b/include/ResourceSelectDialog.h new file mode 100644 index 000000000..0b603e2fd --- /dev/null +++ b/include/ResourceSelectDialog.h @@ -0,0 +1,73 @@ +/* + * ResourceSelectDialog.h - header file for ResourceSelectDialog + * + * Copyright (c) 2009 Tobias Doerffel + * + * This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program (see COPYING); if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + */ + +#ifndef _RESOURCE_SELECT_DIALOG_H +#define _RESOURCE_SELECT_DIALOG_H + +#include + +class ResourceItem; +class ResourceModel; + +class ResourceSelectDialog : public QDialog +{ + Q_OBJECT +public: + enum ModelTypes + { + ListModel, + TreeModel + } ; + + enum DatabaseScope + { + WorkingDirResources, + WebResources, + AllResources + } ; + + ResourceSelectDialog( QWidget * _parent, ModelTypes _modelType, + DatabaseScope _databaseScope = AllResources ); + virtual ~ResourceSelectDialog(); + + // returns the selected item (NULL if the dialog was not accepted or no + // valid item was selected) + ResourceItem * selectedItem(); + + +protected: + void setupUi(); + + +private slots: + void setTypeFilter( int ); + + +private: + ResourceModel * m_model; + +} ; + +#endif + diff --git a/src/gui/QuickLoadDialog.cpp b/src/gui/QuickLoadDialog.cpp index aab67def6..7e64d6641 100644 --- a/src/gui/QuickLoadDialog.cpp +++ b/src/gui/QuickLoadDialog.cpp @@ -23,21 +23,23 @@ */ #include "QuickLoadDialog.h" -#include "ResourceListModel.h" -#include "engine.h" #include "ui_QuickLoadDialog.h" - QuickLoadDialog::QuickLoadDialog( QWidget * _parent, - ResourceItem::Type _typeFilter ) : - QDialog( _parent ), - ui( new Ui::QuickLoadDialog ), - m_listModel( new ResourceListModel( engine::mergedResourceDB(), this ) ) + ResourceItem::Type _typeFilter, + DatabaseScope _databaseScope ) : + ResourceSelectDialog( _parent, ResourceSelectDialog::ListModel, + _databaseScope ), + ui( new Ui::QuickLoadDialog ) { + // setup form ui->setupUi( this ); + // setup ResourceSelectDialog + setupUi(); + // setup type combobox + type filtering for( int i = ResourceItem::TypeUnknown+1; i < ResourceItem::NumTypes; ++i ) { @@ -53,16 +55,6 @@ QuickLoadDialog::QuickLoadDialog( QWidget * _parent, { ui->resourceTypeComboBox->setCurrentIndex( _typeFilter ); } - - // setup list view to display our model - ui->resourceListView->setModel( m_listModel ); - ui->resourceListView->selectionModel()->select( - m_listModel->index( 0, 0 ), - QItemSelectionModel::SelectCurrent ); - - // connect filter edit with model - connect( ui->filterEdit, SIGNAL( textChanged( const QString & ) ), - m_listModel, SLOT( setKeywordFilter( const QString & ) ) ); } @@ -70,18 +62,6 @@ QuickLoadDialog::QuickLoadDialog( QWidget * _parent, QuickLoadDialog::~QuickLoadDialog() { - delete m_listModel; } - - -void QuickLoadDialog::setTypeFilter( int _type ) -{ - m_listModel->setTypeFilter( static_cast( _type ) ); -} - - - -#include "moc_QuickLoadDialog.cxx" - diff --git a/src/gui/ResourceSelectDialog.cpp b/src/gui/ResourceSelectDialog.cpp new file mode 100644 index 000000000..aacd1a598 --- /dev/null +++ b/src/gui/ResourceSelectDialog.cpp @@ -0,0 +1,128 @@ +/* + * ResourceSelectDialog.cpp - implementation of ResourceSelectDialog + * + * Copyright (c) 2009 Tobias Doerffel + * + * This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program (see COPYING); if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + */ + +#include +#include + +#include "ResourceSelectDialog.h" +#include "ResourceListModel.h" +#include "ResourceTreeModel.h" +#include "engine.h" + + + +ResourceSelectDialog::ResourceSelectDialog( QWidget * _parent, + ModelTypes _modelType, + DatabaseScope _databaseScope ) : + QDialog( _parent ), + m_model( NULL ) +{ + ResourceDB * db = NULL; + switch( _databaseScope ) + { + case WorkingDirResources: + db = engine::workingDirResourceDB(); + break; + case WebResources: + db = engine::webResourceDB(); + break; + case AllResources: + default: + db = engine::mergedResourceDB(); + break; + } + + switch( _modelType ) + { + case ListModel: + m_model = new ResourceListModel( db, this ); + break; + case TreeModel: + m_model = new ResourceTreeModel( db, this ); + break; + } +} + + + + +ResourceSelectDialog::~ResourceSelectDialog() +{ + delete m_model; +} + + + + +ResourceItem * ResourceSelectDialog::selectedItem() +{ + if( result() != QDialog::Accepted ) + { + return NULL; + } + + QAbstractItemView * resourceView = findChild(); + if( !resourceView ) + { + return NULL; + } + + return m_model->item( resourceView->currentIndex() ); +} + + + + +void ResourceSelectDialog::setupUi( ) +{ + QAbstractItemView * resourceView = findChild(); + if( resourceView ) + { + // setup view to display our model + resourceView->setModel( m_model ); + resourceView->selectionModel()->select( m_model->index( 0, 0 ), + QItemSelectionModel::SelectCurrent ); + } + + QLineEdit * filterEdit = findChild(); + if( filterEdit ) + { + // connect filter edit with model + connect( filterEdit, SIGNAL( textChanged( const QString & ) ), + m_model, SLOT( setKeywordFilter( const QString & ) ) ); + } +} + + + + +void ResourceSelectDialog::setTypeFilter( int _type ) +{ + m_model->setTypeFilter( static_cast( _type ) ); +} + + + +#include "moc_ResourceSelectDialog.cxx" +