kexi

kexiactionselectiondialog.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005-2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kexiactionselectiondialog.h"
00021 
00022 #include <widget/utils/klistviewitemtemplate.h>
00023 #include <keximainwindow.h>
00024 #include <kexipartitem.h>
00025 #include <kexiproject.h>
00026 
00027 #include <klistview.h>
00028 #include <kaction.h>
00029 #include <kiconloader.h>
00030 #include <kdebug.h>
00031 
00032 #include <qbitmap.h>
00033 #include <qlabel.h>
00034 #include <qheader.h>
00035 #include <qvbox.h>
00036 
00037 typedef KListViewItemTemplate<QString> ActionSelectorDialogListItem;
00038 
00039 class ActionsListView : public KListView
00040 {
00041 public:
00042     ActionsListView(QWidget* parent, KexiActionSelectionDialog* dialog)
00043         : KListView(parent, "actionslistview")
00044     {
00045         setResizeMode(QListView::LastColumn);
00046         addColumn("");
00047         header()->hide();
00048 
00049         QPixmap noIcon( IconSize( KIcon::Small ), IconSize( KIcon::Small ) );
00050         QBitmap bmpNoIcon(noIcon.size());
00051         bmpNoIcon.fill(Qt::color0);
00052         noIcon.setMask(bmpNoIcon);
00053 
00054         QListViewItem *pitem = 0;
00055         KActionPtrList sharedActions( dialog->mainWin()->allActions() ); //sharedActions() );
00056         foreach (KActionPtrList::ConstIterator, it, sharedActions) {
00060             QString name = QString("kaction:%1").arg((*it)->name());
00061             pitem = new ActionSelectorDialogListItem(name, this, pitem, (*it)->text().replace("&", "") );
00062             pitem->setPixmap( 0, (*it)->iconSet( KIcon::Small, 16 ).pixmap( QIconSet::Small, QIconSet::Active ) );
00063             if (!pitem->pixmap(0) || pitem->pixmap(0)->isNull())
00064                 pitem->setPixmap( 0, noIcon );
00065             if (!selectedItem() && dialog->currentActionName() == name)
00066                 setSelected(pitem, true);
00067         }
00068         ensureItemVisible(selectedItem());
00069     }
00070     virtual ~ActionsListView() {}
00071 };
00072 
00073 class ScriptsListView : public KListView
00074 {
00075 public:
00076     ScriptsListView(QWidget* parent, KexiActionSelectionDialog* dialog)
00077         : KListView(parent, "scriptslistview")
00078     {
00079         setResizeMode(QListView::LastColumn);
00080         addColumn("");
00081         header()->hide();
00082 
00083         QPixmap pm( SmallIcon("script") );
00084         KexiPart::Info* info = Kexi::partManager().infoForMimeType("kexi/script");
00085         KexiProject* project = dialog->mainWin()->project();
00086         if(info && project) {
00087             KexiPart::ItemDict* itemdict = project->items(info);
00088             if(itemdict) {
00089                 ActionSelectorDialogListItem* item = 0;
00090                 for (KexiPart::ItemDictIterator it( *itemdict ); it.current(); ++it) {
00091                     QString name = QString("script:%1").arg(it.current()->name());
00092                     item = new ActionSelectorDialogListItem(name, this, item, it.current()->caption());
00093                     item->setPixmap(0, pm);
00094                     if(!selectedItem() && dialog->currentActionName() == name)
00095                         setSelected(item, true);
00096                 }
00097                 ensureItemVisible(selectedItem());
00098             }
00099         }
00100     }
00101     virtual ~ScriptsListView() {}
00102 };
00103 
00104 class KexiActionSelectionDialog::KexiActionSelectionDialogPrivate
00105 {
00106 public:
00107     KexiMainWindow* mainWin;
00108     KListView* kactionListView;
00109     KListView* scriptListView;
00110     QVBox *mainbox;
00111     QString currentActionName;
00112     KexiActionSelectionDialogPrivate() 
00113         : kactionListView(0), scriptListView(0) 
00114     {}
00115 };
00116 
00117 //-------------------------------------
00118 
00120 #define ACTION_TYPE_NO_ACTION_ID 0
00121 #define ACTION_TYPE_KACTION_ID 1
00122 #define ACTION_TYPE_SCRIPT_ID 2
00123 
00124 //-------------------------------------
00125 
00126 KexiActionSelectionDialog::KexiActionSelectionDialog(KexiMainWindow* mainWin, QWidget *parent, 
00127     const QString& _currentActionName, const QCString& actionWidgetName)
00128 
00129     : KDialogBase(parent, "actionSelectorDialog", true, i18n("Assigning Action to Command Button"), 
00130         KDialogBase::Ok | KDialogBase::Cancel )
00131     , d( new KexiActionSelectionDialogPrivate() )
00132 {
00133     d->mainWin = mainWin;
00134     d->currentActionName = _currentActionName;
00135 
00136     QVBox* box = makeVBoxMainWidget();
00137 
00138     QLabel *lbl = new QLabel(i18n("Select Action to be executed after clicking \"%1\" button.")
00139         .arg(actionWidgetName), box);
00140     lbl->setAlignment(Qt::AlignTop|Qt::AlignLeft|Qt::WordBreak);
00141 
00142     QWidget *w = new QWidget(box);
00143     QHBoxLayout *lyr = new QHBoxLayout(w, 0, KDialogBase::spacingHint());
00144     QComboBox* combobox = new QComboBox(w);
00145     combobox->insertItem( i18n("No Action") );
00146     combobox->insertItem( i18n("Application") );
00147     lbl = new QLabel(combobox, i18n("Action type:").arg(actionWidgetName), w);
00148     lyr->addWidget(lbl);
00149     lyr->addWidget(combobox);
00150     lyr->addStretch(1);
00151     
00152     // We use the scriptpart to determinate if the Kexi ScriptPart-plugin is
00153     // installed and if we like to show it in our list of actions.
00154     KexiPart::Info* scriptinfo = Kexi::partManager().infoForMimeType("kexi/script");
00155     if(scriptinfo) {
00156         combobox->insertItem( i18n("Scripts") );
00157     }
00158     
00159     d->mainbox = new QVBox(box);
00160     box->setStretchFactor(d->mainbox, 1);
00161 
00162     resize(400, 500);
00163 
00164     if (d->currentActionName.startsWith("script:")) {
00165         if(scriptinfo) {
00166             combobox->setCurrentItem(ACTION_TYPE_SCRIPT_ID);
00167             slotActionTypeSelected(ACTION_TYPE_SCRIPT_ID);
00168         }
00169         
00170     }
00171     else { // default == kaction //if (d->currentActionName.startsWith("kaction:")) {
00172         combobox->setCurrentItem(ACTION_TYPE_KACTION_ID);
00173         slotActionTypeSelected(ACTION_TYPE_KACTION_ID);
00174     }
00175     connect(combobox, SIGNAL(activated(int)), this, SLOT(slotActionTypeSelected(int)));
00176     connect(this, SIGNAL(finished()), SLOT(closeDialog()));
00177 }
00178 
00179 KexiActionSelectionDialog::~KexiActionSelectionDialog()
00180 {
00181     delete d;
00182 }
00183 
00184 void KexiActionSelectionDialog::showKActionListView()
00185 {
00186     if (d->scriptListView)
00187         d->scriptListView->hide();
00188     if (!d->kactionListView) {
00189         d->kactionListView = new ActionsListView(d->mainbox, this);
00190     }
00191     d->kactionListView->show();
00192 }
00193 
00194 void KexiActionSelectionDialog::showScriptListView()
00195 {
00196     if (d->kactionListView)
00197         d->kactionListView->hide();
00198     if (!d->scriptListView) {
00199         d->scriptListView = new ScriptsListView(d->mainbox, this);
00200     }
00201     d->scriptListView->show();
00202 }
00203 
00204 void KexiActionSelectionDialog::slotActionTypeSelected(int index)
00205 {
00206     switch(index) {
00207         case ACTION_TYPE_NO_ACTION_ID:
00208             if (d->kactionListView)
00209                 d->kactionListView->hide();
00210             if (d->scriptListView)
00211                 d->scriptListView->hide();
00212             break;
00213         case ACTION_TYPE_KACTION_ID:
00214             showKActionListView();
00215             break;
00216         case ACTION_TYPE_SCRIPT_ID:
00217             showScriptListView();
00218             break;
00219         default:;
00220     }
00221 }
00222 
00223 KexiMainWindow* KexiActionSelectionDialog::mainWin()
00224 {
00225     return d->mainWin;
00226 }
00227 
00228 QString KexiActionSelectionDialog::currentActionName() const
00229 {
00230     return d->currentActionName;
00231 }
00232 
00233 void KexiActionSelectionDialog::slotOk()
00234 {
00235     QListViewItem *item = 
00236         (d->kactionListView && d->kactionListView->isVisible()) ? d->kactionListView->selectedItem() : 0;
00237     if (!item)
00238         item = (d->scriptListView && d->scriptListView->isVisible()) ? d->scriptListView->selectedItem() : 0;
00239     if (item) {
00240         d->currentActionName = dynamic_cast<ActionSelectorDialogListItem*>( item )->data;
00241     }
00242     else {
00243         d->currentActionName = QString::null; // "No Action"
00244     }
00245     KDialogBase::slotOk();
00246 }
00247 
00248 void KexiActionSelectionDialog::closeDialog()
00249 {
00250     // not needed cause KexiFormPart which uses us takes care of freeing.
00251     //delayedDestruct();
00252 }
00253 
00254 #include "kexiactionselectiondialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys