File indexing completed on 2024-04-14 04:00:24

0001 //
0002 // C++ Implementation: dlgvarviewer
0003 //
0004 // Description: 
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 "dlgvarviewer.h"
0024 
0025 #include "cactionmanager.h"
0026 #include "cvariablelist.h"
0027 
0028 #include <KLocalizedString>
0029 
0030 #include <QAbstractTableModel>
0031 #include <QTreeView>
0032 
0033 #include <map>
0034 
0035 // this model holds and presents the variables and their values
0036 class VariableModel : public QAbstractTableModel {
0037  public:
0038   VariableModel () {
0039     list = nullptr;
0040     count = 0;
0041   }
0042 
0043   int columnCount (const QModelIndex &parent = QModelIndex()) const override {
0044     if (parent.isValid()) return 0;
0045     return 2;
0046   }
0047 
0048   int rowCount (const QModelIndex &parent = QModelIndex()) const override {
0049     if (parent.isValid()) return 0;
0050     return count;
0051   }
0052 
0053   QVariant data (const QModelIndex &index, int role = Qt::DisplayRole) const override
0054   {
0055     if (role != Qt::DisplayRole) return QVariant();
0056     if (index.parent().isValid()) return QVariant();
0057     int row = index.row();
0058     int col = index.column();
0059     if ((row < 0) || (row >= count)) return QVariant();
0060     if ((col < 0) || (col >= 2)) return QVariant();
0061     QString name = vars[row];
0062     if (col == 0) return name;
0063     return list->getValue (name);
0064   }
0065 
0066   QVariant headerData (int section, Qt::Orientation, int role = Qt::DisplayRole) const override
0067   {
0068     if (role != Qt::DisplayRole) return QVariant();
0069     switch (section) {
0070       case 0: return i18n ("Variable");
0071       case 1: return i18n ("Value");
0072       default: return QVariant();
0073     }
0074   }
0075 
0076   void listChanged (cVariableList *newList) {
0077     beginResetModel();
0078     pos.clear ();
0079     count = 0;
0080     list = newList;
0081     if (list) {
0082       // load variable data
0083       vars = list->getList ();
0084       for (QStringList::iterator it = vars.begin(); it != vars.end(); ++it)
0085         pos[*it] = count++;
0086     }
0087     endResetModel();
0088   }
0089 
0090   void variableChanged (const QString &name) {
0091     if (!list) return;
0092     // check possible situations
0093     if (!list->exists (name)) {
0094       // variable just got deleted
0095       // TODO: only remove the correct row instead of resetting everything
0096       listChanged (list);
0097     } else if (!pos.count (name)) {
0098       // variable just got created
0099       // TODO: only add the correct row instead of resetting everything
0100       listChanged (list);
0101     } else {
0102       // an existing variable was changed
0103       int row = pos[name];
0104       emit dataChanged (index (row, 0, QModelIndex()), index (row, 1, QModelIndex()));
0105     }
0106 
0107   }
0108 
0109  private:
0110   cVariableList *list;
0111   std::map<QString, int> pos;  // positions of variables
0112   QStringList vars;
0113   int count;
0114 };
0115 
0116 
0117 dlgVarViewer::dlgVarViewer (QWidget *parent) : QDockWidget (parent), cActionBase ("variable-viewer", 0)
0118 {
0119   model = new VariableModel;
0120   createDialog ();
0121 
0122   addEventHandler ("connected", 200, PT_NOTHING);
0123   addEventHandler ("disconnected", 200, PT_NOTHING);
0124   addEventHandler ("session-activated", 200, PT_INT);
0125   addEventHandler ("var-changed", 200, PT_STRING);
0126 }
0127 
0128 dlgVarViewer::~dlgVarViewer ()
0129 {
0130   removeEventHandler ("connected");
0131   removeEventHandler ("disconnected");
0132   removeEventHandler ("session-activated");
0133   removeEventHandler ("var-changed");
0134 
0135   delete model;
0136 }
0137 
0138 void dlgVarViewer::createDialog ()
0139 {
0140   // setInitialSize (QSize (300, 200));
0141   setWindowTitle (i18n ("Variables"));
0142 
0143   viewer = new QTreeView (this);
0144   viewer->setAllColumnsShowFocus (true);
0145   viewer->setRootIsDecorated (false);
0146   viewer->setUniformRowHeights (true);
0147   viewer->setModel (model);
0148 
0149   setWidget (viewer);
0150   
0151   //no focus - we don't want this dialog to get focus
0152   setFocusPolicy (Qt::NoFocus);
0153   viewer->setFocusPolicy (Qt::NoFocus);
0154 }
0155 
0156 void dlgVarViewer::eventNothingHandler (QString event, int sess)
0157 {
0158   if (!isVisible()) return;  // nothing if we aren't shown
0159   cActionManager *am = cActionManager::self();
0160   if (am->activeSession() != sess) return;  // nothing if it's not the active session
0161 
0162   if (event == "connected") {
0163     cActionManager *am = cActionManager::self();
0164     cVariableList *vars = dynamic_cast<cVariableList *>(am->object ("variables", sess));
0165     model->listChanged (vars);
0166   }
0167   if (event == "disconnected") {
0168     model->listChanged (nullptr);
0169   }
0170 }
0171 
0172 void dlgVarViewer::eventIntHandler (QString event, int, int val, int)
0173 {
0174   if (!isVisible()) return;  // nothing if we aren't shown
0175 
0176   if (event == "session-activated") {
0177     cActionManager *am = cActionManager::self();
0178     cVariableList *vars = dynamic_cast<cVariableList *>(am->object ("variables", val));
0179     model->listChanged (vars);
0180   }
0181 }
0182 
0183 void dlgVarViewer::eventStringHandler (QString event, int sess, QString &par1, const QString &)
0184 {
0185   if (!isVisible()) return;  // nothing if we aren't shown
0186   cActionManager *am = cActionManager::self();
0187   if (am->activeSession() != sess) return;  // nothing if it's not the active session
0188 
0189   if (event == "var-changed") {
0190     model->variableChanged (par1);
0191   }
0192 }
0193 
0194 void dlgVarViewer::showEvent (QShowEvent *)
0195 {
0196   // when the dialog is shown, we need to reset the data
0197   cActionManager *am = cActionManager::self();
0198   int sess = am->activeSession ();
0199   cVariableList *vars = dynamic_cast<cVariableList *>(am->object ("variables", sess));
0200   model->listChanged (vars);
0201 }
0202 
0203 #include "moc_dlgvarviewer.cpp"