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 KexiPartItemsListView : public KListView
00074 {
00075 public:
00076     KexiPartItemsListView(QWidget* parent, KexiActionSelectionDialog* dialog, const QString& partname)
00077         : KListView(parent)
00078     {
00079         setResizeMode(QListView::LastColumn);
00080         addColumn("");
00081         header()->hide();
00082 
00083         QPixmap pm( SmallIcon(partname) );
00084         KexiPart::Info* info = Kexi::partManager().infoForMimeType( QString("kexi/%1").arg(partname) );
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("%1:%2").arg(partname).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 ~KexiPartItemsListView() {}
00102 };
00103 
00104 class MacrosListView : public KexiPartItemsListView
00105 {
00106 public:
00107     MacrosListView(QWidget* parent, KexiActionSelectionDialog* dialog)
00108         : KexiPartItemsListView(parent, dialog, "macro") {}
00109     virtual ~MacrosListView() {}
00110 };
00111 
00112 class ScriptsListView : public KexiPartItemsListView
00113 {
00114 public:
00115     ScriptsListView(QWidget* parent, KexiActionSelectionDialog* dialog)
00116         : KexiPartItemsListView(parent, dialog, "script") {}
00117     virtual ~ScriptsListView() {}
00118 };
00119 
00120 class KexiActionSelectionDialog::KexiActionSelectionDialogPrivate
00121 {
00122 public:
00123     KexiMainWindow* mainWin;
00124     ActionsListView* kactionListView;
00125     MacrosListView* macroListView;
00126     ScriptsListView* scriptListView;
00127     QVBox *mainbox;
00128     QString currentActionName;
00129     KexiActionSelectionDialogPrivate() 
00130         : kactionListView(0), macroListView(0), scriptListView(0) 
00131     {}
00132 };
00133 
00134 //-------------------------------------
00135 
00137 #define ACTION_TYPE_NO_ACTION_ID 0
00138 #define ACTION_TYPE_KACTION_ID 1
00139 #define ACTION_TYPE_MACRO_ID 2
00140 #define ACTION_TYPE_SCRIPT_ID 3
00141 
00142 //-------------------------------------
00143 
00144 KexiActionSelectionDialog::KexiActionSelectionDialog(KexiMainWindow* mainWin, QWidget *parent, 
00145     const QString& _currentActionName, const QCString& actionWidgetName)
00146 
00147     : KDialogBase(parent, "actionSelectorDialog", true, i18n("Assigning Action to Command Button"), 
00148         KDialogBase::Ok | KDialogBase::Cancel )
00149     , d( new KexiActionSelectionDialogPrivate() )
00150 {
00151     d->mainWin = mainWin;
00152     d->currentActionName = _currentActionName;
00153 
00154     QVBox* box = makeVBoxMainWidget();
00155 
00156     QLabel *lbl = new QLabel(i18n("Select Action to be executed after clicking \"%1\" button.")
00157         .arg(actionWidgetName), box);
00158     lbl->setAlignment(Qt::AlignTop|Qt::AlignLeft|Qt::WordBreak);
00159 
00160     QWidget *w = new QWidget(box);
00161     QHBoxLayout *lyr = new QHBoxLayout(w, 0, KDialogBase::spacingHint());
00162     QComboBox* combobox = new QComboBox(w);
00163     combobox->insertItem( i18n("No Action") );
00164     combobox->insertItem( i18n("Application") );
00165     lbl = new QLabel(combobox, i18n("Action type:").arg(actionWidgetName), w);
00166     lyr->addWidget(lbl);
00167     lyr->addWidget(combobox);
00168     lyr->addStretch(1);
00169 
00170     d->mainbox = new QVBox(box);
00171     box->setStretchFactor(d->mainbox, 1);
00172     resize(400, 500);
00173 
00174     // We use the PartManager to determinate if the Kexi-plugin is
00175     // installed and if we like to show it in our list of actions.
00176     KexiPart::Info* macroinfo = Kexi::partManager().infoForMimeType("kexi/macro");
00177     if(macroinfo) {
00178         combobox->insertItem( i18n("Macros") );
00179     }
00180 
00181     KexiPart::Info* scriptinfo = Kexi::partManager().infoForMimeType("kexi/script");
00182     if(scriptinfo) {
00183         combobox->insertItem( i18n("Scripts") );
00184     }
00185 
00186     if (d->currentActionName.startsWith("macro:")) {
00187         if(macroinfo) {
00188             combobox->setCurrentItem(ACTION_TYPE_MACRO_ID);
00189             slotActionTypeSelected(ACTION_TYPE_MACRO_ID);
00190         }
00191         
00192     }
00193     else if (d->currentActionName.startsWith("script:")) {
00194         if(scriptinfo) {
00195             combobox->setCurrentItem(ACTION_TYPE_SCRIPT_ID);
00196             slotActionTypeSelected(ACTION_TYPE_SCRIPT_ID);
00197         }
00198         
00199     }
00200     else { // default == kaction //if (d->currentActionName.startsWith("kaction:")) {
00201         combobox->setCurrentItem(ACTION_TYPE_KACTION_ID);
00202         slotActionTypeSelected(ACTION_TYPE_KACTION_ID);
00203     }
00204     connect(combobox, SIGNAL(activated(int)), this, SLOT(slotActionTypeSelected(int)));
00205     connect(this, SIGNAL(finished()), SLOT(closeDialog()));
00206 }
00207 
00208 KexiActionSelectionDialog::~KexiActionSelectionDialog()
00209 {
00210     delete d;
00211 }
00212 
00213 void KexiActionSelectionDialog::showKActionListView()
00214 {
00215     if (d->macroListView)
00216         d->macroListView->hide();
00217     if (d->scriptListView)
00218         d->scriptListView->hide();
00219     if (!d->kactionListView) {
00220         d->kactionListView = new ActionsListView(d->mainbox, this);
00221     }
00222     d->kactionListView->show();
00223 }
00224 
00225 void KexiActionSelectionDialog::showMacroListView()
00226 {
00227     if (d->kactionListView)
00228         d->kactionListView->hide();
00229     if (d->scriptListView)
00230         d->scriptListView->hide();
00231     if (!d->macroListView) {
00232         d->macroListView = new MacrosListView(d->mainbox, this);
00233     }
00234     d->macroListView->show();
00235 }
00236 
00237 void KexiActionSelectionDialog::showScriptListView()
00238 {
00239     if (d->kactionListView)
00240         d->kactionListView->hide();
00241     if (d->macroListView)
00242         d->macroListView->hide();
00243     if (!d->scriptListView) {
00244         d->scriptListView = new ScriptsListView(d->mainbox, this);
00245     }
00246     d->scriptListView->show();
00247 }
00248 
00249 void KexiActionSelectionDialog::slotActionTypeSelected(int index)
00250 {
00251     switch(index) {
00252         case ACTION_TYPE_NO_ACTION_ID:
00253             if (d->kactionListView)
00254                 d->kactionListView->hide();
00255             if (d->macroListView)
00256                 d->macroListView->hide();
00257             if (d->scriptListView)
00258                 d->scriptListView->hide();
00259             break;
00260         case ACTION_TYPE_KACTION_ID:
00261             showKActionListView();
00262             break;
00263         case ACTION_TYPE_MACRO_ID:
00264             showMacroListView();
00265             break;
00266         case ACTION_TYPE_SCRIPT_ID:
00267             showScriptListView();
00268             break;
00269         default:;
00270     }
00271 }
00272 
00273 KexiMainWindow* KexiActionSelectionDialog::mainWin()
00274 {
00275     return d->mainWin;
00276 }
00277 
00278 QString KexiActionSelectionDialog::currentActionName() const
00279 {
00280     return d->currentActionName;
00281 }
00282 
00283 void KexiActionSelectionDialog::slotOk()
00284 {
00285     QListViewItem *item = 
00286         (d->kactionListView && d->kactionListView->isVisible()) ? d->kactionListView->selectedItem() : 0;
00287     if (!item)
00288         item = (d->macroListView && d->macroListView->isVisible()) ? d->macroListView->selectedItem() : 0;
00289     if (!item)
00290         item = (d->scriptListView && d->scriptListView->isVisible()) ? d->scriptListView->selectedItem() : 0;
00291     if (item) {
00292         d->currentActionName = dynamic_cast<ActionSelectorDialogListItem*>( item )->data;
00293     }
00294     else {
00295         d->currentActionName = QString::null; // "No Action"
00296     }
00297     KDialogBase::slotOk();
00298 }
00299 
00300 void KexiActionSelectionDialog::closeDialog()
00301 {
00302     // not needed cause KexiFormPart which uses us takes care of freeing.
00303     //delayedDestruct();
00304 }
00305 
00306 #include "kexiactionselectiondialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys