File indexing completed on 2024-04-28 04:02:31

0001 //
0002 // C++ Interface: dlgobjects
0003 //
0004 // Description: Object Manager
0005 //
0006 /*
0007 Copyright 2008-2011 Tomas Mecir <kmuddy@kmuddy.com>
0008 
0009 This program is free software; you can redistribute it and/or
0010 modify it under the terms of the GNU General Public License as
0011 published by the Free Software Foundation; either version 2 of 
0012 the License, or (at your option) any later version.
0013 
0014 This program is distributed in the hope that it will be useful,
0015 but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017 GNU General Public License for more details.
0018 
0019 You should have received a copy of the GNU General Public License
0020 along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include "dlgobjects.h"
0024 
0025 #include "cactionmanager.h"
0026 #include "clist.h"
0027 #include "clisteditor.h"
0028 #include "clistgroupeditor.h"
0029 #include "clistmanager.h"
0030 #include "clistviewer.h"
0031 
0032 #include <kactioncollection.h>
0033 #include <KLocalizedString>
0034 #include <ktoolbar.h>
0035 
0036 #include <QComboBox>
0037 #include <QDebug>
0038 #include <QDialogButtonBox>
0039 #include <QSplitter>
0040 #include <QStackedWidget>
0041 #include <QStandardItemModel>
0042 #include <QVBoxLayout>
0043 
0044 struct dlgObjects::Private
0045 {
0046   QComboBox *lists;
0047   KToolBar *toolbar;
0048   QStackedWidget *editorStack;
0049   cListEditor *objEditor, *groupEditor;
0050   QWidget *emptyEditor;
0051   cListViewer *viewer;
0052   QString currentList;
0053   int currentSession;
0054 
0055   QAction *aUp, *aDown, *aLeft, *aRight;
0056 };
0057 
0058 dlgObjects::dlgObjects (QWidget *parent)
0059   : QDialog (parent), cActionBase ("dialog-objects", 0)
0060 {
0061   d = new Private;
0062 
0063   //initial dialog size
0064   setWindowTitle (i18n ("Object Manager"));
0065 
0066   //create main dialog's widget
0067   QSplitter *page = new QSplitter (this);
0068   QVBoxLayout *mainLayout = new QVBoxLayout (this);
0069 
0070   QWidget *left = new QWidget (page);
0071   d->editorStack = new QStackedWidget (page);
0072 
0073   QVBoxLayout *layout = new QVBoxLayout (left);
0074 
0075   layout->setSpacing (5);
0076 
0077   d->toolbar = new KToolBar (left, false, false);
0078   d->toolbar->setToolButtonStyle (Qt::ToolButtonIconOnly);
0079 
0080   d->lists = new QComboBox (left);
0081   d->viewer = new cListViewer (left);
0082 
0083   d->emptyEditor = new QWidget (d->editorStack);
0084   d->objEditor = nullptr;
0085   d->groupEditor = new cListGroupEditor (d->editorStack);
0086   d->editorStack->addWidget (d->emptyEditor);
0087   d->editorStack->addWidget (d->groupEditor);
0088 
0089   d->aUp = new QAction (this);
0090   d->aUp->setText (i18n ("Up"));
0091   d->aUp->setIcon (QIcon::fromTheme ("arrow-up"));
0092   connect (d->aUp, &QAction::triggered, d->viewer, &cListViewer::moveUp);
0093   d->aDown = new QAction(this);
0094   d->aDown->setText (i18n ("Down"));
0095   d->aDown->setIcon (QIcon::fromTheme ("arrow-down"));
0096   connect (d->aDown, &QAction::triggered, d->viewer, &cListViewer::moveDown);
0097   d->aLeft = new QAction (this);
0098   d->aLeft->setText (i18n ("Left"));
0099   d->aLeft->setIcon (QIcon::fromTheme ("arrow-left"));
0100   connect (d->aLeft, &QAction::triggered, d->viewer, &cListViewer::moveLeft);
0101   d->aRight = new QAction (this);
0102   d->aRight->setText (i18n ("Right"));
0103   d->aRight->setIcon (QIcon::fromTheme ("arrow-right"));
0104   connect (d->aRight, &QAction::triggered, d->viewer, &cListViewer::moveRight);
0105 
0106   KActionCollection *col = d->viewer->actionCollection ();
0107   d->toolbar->addAction (col->action ("AddGroup"));
0108   d->toolbar->addAction (col->action ("AddObject"));
0109   d->toolbar->addAction (col->action ("DeleteObject"));
0110 
0111   d->toolbar->addSeparator ();
0112   d->toolbar->addAction (d->aUp);
0113   d->toolbar->addAction (d->aDown);
0114   d->toolbar->addAction (d->aLeft);
0115   d->toolbar->addAction (d->aRight);
0116 
0117   layout->addWidget (d->lists);
0118   layout->addWidget (d->toolbar);
0119   layout->addWidget (d->viewer);
0120 
0121   page->addWidget (left);
0122   page->addWidget (d->editorStack);
0123 
0124   QDialogButtonBox *buttons = new QDialogButtonBox (QDialogButtonBox::Close, this);
0125   connect (buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
0126 
0127   mainLayout->addWidget (page);
0128   mainLayout->addWidget (buttons);
0129 
0130   connect (d->lists, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &dlgObjects::listChanged);
0131   connect (d->viewer, &cListViewer::itemActivated, this, &dlgObjects::activeObjectChanged);
0132   connect (this, &QDialog::accepted, this, &dlgObjects::saveChanges);
0133   connect (this, &QDialog::rejected, this, &dlgObjects::saveChanges);
0134 
0135   d->currentSession = cActionManager::self()->activeSession ();
0136 
0137   d->lists->setModel (cListManager::self()->typeModel());
0138 
0139   addEventHandler ("connected", 50, PT_NOTHING);
0140   addEventHandler ("disconnected", 50, PT_NOTHING);
0141   addEventHandler ("session-activated", 50, PT_INT);
0142 }
0143 
0144 dlgObjects::~dlgObjects ()
0145 {
0146   removeEventHandler ("session-activated");
0147   removeEventHandler ("connected");
0148   removeEventHandler ("disconnected");
0149 
0150   delete d;
0151 }
0152 
0153 QSize dlgObjects::sizeHint() const
0154 {
0155   return QSize (600, 400);
0156 }
0157 
0158 void dlgObjects::eventNothingHandler (QString event, int session)
0159 {
0160   if (event == "connected") {
0161     if (d->currentSession == session)
0162       switchList ();
0163   }
0164   else if (event == "disconnected") {
0165     if (d->currentSession == session)
0166       switchList ();
0167   }
0168 }
0169 
0170 void dlgObjects::eventIntHandler (QString event, int, int par1, int)
0171 {
0172   if (event == "session-activated") {
0173     d->currentSession = par1;
0174     switchList ();
0175   }
0176 }
0177 
0178 void dlgObjects::listChanged (int index)
0179 {
0180   // save changes in the current object, if any
0181   saveChanges ();
0182 
0183   QStandardItemModel *model = cListManager::self()->typeModel();
0184   QString listName = model->item (index)->data().toString();
0185   d->currentList = listName;
0186   switchList ();
0187 }
0188 
0189 void dlgObjects::switchList ()
0190 {
0191   cList *list = cListManager::self()->getList (d->currentSession, d->currentList);
0192 
0193   if (d->objEditor) {
0194     d->editorStack->removeWidget (d->objEditor);
0195     delete d->objEditor;
0196     d->objEditor = nullptr;
0197   }
0198   if (list) {
0199     d->objEditor = list->editor (this);
0200     d->editorStack->addWidget (d->objEditor);
0201   }
0202   d->editorStack->setCurrentWidget (d->emptyEditor);
0203   
0204   d->viewer->setList (list);
0205 
0206   qDebug() << "Switching active list: " << d->currentList << " in session " << d->currentSession;
0207 }
0208 
0209 void dlgObjects::saveChanges ()
0210 {
0211   cListEditor *editor = dynamic_cast<cListEditor *>(d->editorStack->currentWidget ());
0212   if (!editor) return;  // no object editor is active
0213 
0214   if (editor->changed ()) {
0215     // TODO: ask the user ?
0216     editor->saveChanges ();
0217   }
0218 }
0219 
0220 void dlgObjects::activeObjectChanged (cListObject *obj)
0221 {
0222   // save changes in the current object, if any
0223   saveChanges ();
0224 
0225   // clear the current object from editors
0226   d->groupEditor->setObject (nullptr);
0227   d->objEditor->setObject (nullptr);
0228 
0229   if ((!obj) || (obj == (cListObject *) obj->list()->rootGroup())) {
0230     // no object - present the empty widget
0231     d->editorStack->setCurrentWidget (d->emptyEditor);
0232     return;
0233   }
0234 
0235   // fill in the proper editor
0236   cListEditor *editor = obj->isGroup() ? d->groupEditor : d->objEditor;
0237   editor->setObject (obj);
0238 
0239   // activate the proper editor
0240   d->editorStack->setCurrentWidget (editor);
0241 
0242   qDebug () << "Switched active object to " << obj->name() << ".";
0243 }
0244 
0245 #include "moc_dlgobjects.cpp"