File indexing completed on 2023-09-24 04:17:34

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 <klocale.h>
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     pos.clear ();
0078     count = 0;
0079     list = newList;
0080     if (list) {
0081       // load variable data
0082       vars = list->getList ();
0083       for (QStringList::iterator it = vars.begin(); it != vars.end(); ++it)
0084         pos[*it] = count++;
0085     }
0086     reset ();
0087   }
0088 
0089   void variableChanged (const QString &name) {
0090     if (!list) return;
0091     // check possible situations
0092     if (!list->exists (name)) {
0093       // variable just got deleted
0094       // TODO: only remove the correct row instead of resetting everything
0095       listChanged (list);
0096     } else if (!pos.count (name)) {
0097       // variable just got created
0098       // TODO: only add the correct row instead of resetting everything
0099       listChanged (list);
0100     } else {
0101       // an existing variable was changed
0102       int row = pos[name];
0103       emit dataChanged (index (row, 0, QModelIndex()), index (row, 1, QModelIndex()));
0104     }
0105 
0106   }
0107 
0108  private:
0109   cVariableList *list;
0110   std::map<QString, int> pos;  // positions of variables
0111   QStringList vars;
0112   int count;
0113 };
0114 
0115 
0116 dlgVarViewer::dlgVarViewer (QWidget *parent) : QDockWidget (parent), cActionBase ("variable-viewer", 0)
0117 {
0118   model = new VariableModel;
0119   createDialog ();
0120 
0121   addEventHandler ("connected", 200, PT_NOTHING);
0122   addEventHandler ("disconnected", 200, PT_NOTHING);
0123   addEventHandler ("session-activated", 200, PT_INT);
0124   addEventHandler ("var-changed", 200, PT_STRING);
0125 }
0126 
0127 dlgVarViewer::~dlgVarViewer ()
0128 {
0129   removeEventHandler ("connected");
0130   removeEventHandler ("disconnected");
0131   removeEventHandler ("session-activated");
0132   removeEventHandler ("var-changed");
0133 
0134   delete model;
0135 }
0136 
0137 void dlgVarViewer::createDialog ()
0138 {
0139   // setInitialSize (QSize (300, 200));
0140   setWindowTitle (i18n ("Variables"));
0141 
0142   viewer = new QTreeView (this);
0143   viewer->setAllColumnsShowFocus (true);
0144   viewer->setRootIsDecorated (false);
0145   viewer->setUniformRowHeights (true);
0146   viewer->setModel (model);
0147 
0148   setWidget (viewer);
0149   
0150   //no focus - we don't want this dialog to get focus
0151   setFocusPolicy (Qt::NoFocus);
0152   viewer->setFocusPolicy (Qt::NoFocus);
0153 }
0154 
0155 void dlgVarViewer::eventNothingHandler (QString event, int sess)
0156 {
0157   if (!isVisible()) return;  // nothing if we aren't shown
0158   cActionManager *am = cActionManager::self();
0159   if (am->activeSession() != sess) return;  // nothing if it's not the active session
0160 
0161   if (event == "connected") {
0162     cActionManager *am = cActionManager::self();
0163     cVariableList *vars = dynamic_cast<cVariableList *>(am->object ("variables", sess));
0164     model->listChanged (vars);
0165   }
0166   if (event == "disconnected") {
0167     model->listChanged (nullptr);
0168   }
0169 }
0170 
0171 void dlgVarViewer::eventIntHandler (QString event, int, int val, int)
0172 {
0173   if (!isVisible()) return;  // nothing if we aren't shown
0174 
0175   if (event == "session-activated") {
0176     cActionManager *am = cActionManager::self();
0177     cVariableList *vars = dynamic_cast<cVariableList *>(am->object ("variables", val));
0178     model->listChanged (vars);
0179   }
0180 }
0181 
0182 void dlgVarViewer::eventStringHandler (QString event, int sess, QString &par1, const QString &)
0183 {
0184   if (!isVisible()) return;  // nothing if we aren't shown
0185   cActionManager *am = cActionManager::self();
0186   if (am->activeSession() != sess) return;  // nothing if it's not the active session
0187 
0188   if (event == "var-changed") {
0189     model->variableChanged (par1);
0190   }
0191 }
0192 
0193 void dlgVarViewer::showEvent (QShowEvent *)
0194 {
0195   // when the dialog is shown, we need to reset the data
0196   cActionManager *am = cActionManager::self();
0197   int sess = am->activeSession ();
0198   cVariableList *vars = dynamic_cast<cVariableList *>(am->object ("variables", sess));
0199   model->listChanged (vars);
0200 }
0201 
0202 #include "moc_dlgvarviewer.cpp"