oopapidocs  2.0
actiontablemodel.cpp
00001 #include <QApplication>
00002 #include <QMessageBox>
00003 #include "actiontablemodel.h"
00004 #include <QAction>
00005 
00006 static const int COLUMNS(3);
00007 
00008 ActionTableModel::
00009 ActionTableModel(QList<QAction*> actions, QObject* parent) :
00010 QAbstractTableModel(parent), m_actions(actions), m_columns(COLUMNS) {}
00011 
00012 
00013 QVariant ActionTableModel::
00014 headerData(int section, Qt::Orientation orientation, int role) const {
00015     if (orientation == Qt::Vertical) return QVariant();
00016     if (role != Qt::DisplayRole) return QVariant();
00017     switch (section)  {
00018     case 2: return tr("Context");
00019         break;
00020     case 1: return tr("Shortcut");
00021         break;
00022     case 0: return tr("Action");
00023         break;
00024     default:
00025         return QVariant();
00026     }
00027 }
00028 
00029 
00030 QAction* ActionTableModel::action(int row) const {
00031     return m_actions[row];
00032 }
00033 
00034 //start id=data
00035 QVariant ActionTableModel::
00036 data(const QModelIndex& index, int role) const {
00037     int row = index.row();
00038     if (row >= m_actions.size()) return QVariant();
00039     int col = index.column();
00040     if (col >= columnCount()) return QVariant();
00041     if (role == Qt::DecorationRole)  
00042         if (col == 0)
00043             return m_actions[row]->icon();
00044     
00045     if (role == Qt::ToolTipRole) {
00046         return m_actions[row]->toolTip();
00047     }
00048     if (role == Qt::StatusTipRole) {
00049         return m_actions[row]->statusTip();
00050     }
00051     if (role == Qt::DisplayRole) {
00052         if (col == 1) return m_actions[row]->shortcut();
00053         if (col == 2) return m_actions[row]->parent()->objectName();
00054         else return m_actions[row]->text();
00055     }
00056     return QVariant();
00057 }
00058 //end
00059 //start id=flags
00060 Qt::ItemFlags ActionTableModel::
00061 flags(const QModelIndex& index) const {
00062     if (index.isValid()) return Qt::ItemIsEnabled;
00063     else return 0;
00064 }
00065 //end
00066 //start id=setdata
00067 bool ActionTableModel::
00068 setData(const QModelIndex& index, const QVariant& value, int role) {
00069     if (role != Qt::EditRole) return false;
00070     int row = index.row();
00071     if ((row < 0) | (row >= m_actions.size())) return false;
00072     QString str = value.toString();
00073     QKeySequence ks(str);
00074     QAction* previousAction = 0;
00075 
00076     if (ks != QKeySequence() ) foreach (QAction* act, m_actions) {
00077         if (act->shortcut() == ks) {
00078             previousAction = act;
00079             break;
00080         }
00081     }
00082     if (previousAction != 0) {
00083         QString error = tr("%1 is already bound to %2.").
00084                 arg(ks.toString()).arg(previousAction->text());
00085         bool answer = QMessageBox::question(0, error,
00086                     tr("%1\n Remove previous binding?").arg(error),
00087                     QMessageBox::Yes, QMessageBox::No);
00088         if (!answer) return false;
00089         previousAction->setShortcut(QKeySequence());
00090     }
00091     m_actions[row]->setShortcut(ks);
00092     QModelIndex changedIdx = createIndex(row, 1);   /* Column 1 displays the shortcut. */
00093     emit dataChanged(changedIdx, changedIdx);       /* Required for Views to know when/what to update. */
00094     return true;
00095 }
00096 //end
 All Classes Namespaces Functions Enumerations