1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
#include <BALL/VIEW/WIDGETS/shortcutTableView.h>
#ifndef BALL_VIEW_KERNEL_SHORTCUTREGISTRY_H
# include <BALL/VIEW/KERNEL/shortcutRegistry.h>
#endif
#ifndef BALL_VIEW_DIALOGS_EDITSINGLESHORTCUT_H
# include <BALL/VIEW/DIALOGS/editSingleShortcut.h>
#endif
#include <QtCore/QAbstractTableModel>
#include <QtGui/QFontMetrics>
#include <QtCore/QSortFilterProxyModel>
#include <BALL/VIEW/WIDGETS/scene.h>
Q_DECLARE_METATYPE(BALL::String)
namespace BALL
{
namespace VIEW
{
////////////////////////// MODEL ////////////////////////////
ShortcutTableModel::ShortcutTableModel(ShortcutRegistry* reg)
: registry_(reg)
{
if (!registry_)
{
throw Exception::NullPointer(__FILE__, __LINE__);
}
}
int ShortcutTableModel::rowCount(const QModelIndex& /*parent*/) const
{
return registry_->size();
}
int ShortcutTableModel::columnCount(const QModelIndex& /*parent*/) const
{
return 2;
}
QVariant ShortcutTableModel::data(const QModelIndex& index, int role) const
{
if(index.column() == 0) {
switch(role) {
case Qt::DisplayRole: {
QString desc = (*registry_)[index.row()].first.c_str();
desc.replace("Shortcut|", "");
desc.replace("|","->");
desc.replace("_"," ");
return QVariant::fromValue(desc);
}
case Qt::DecorationRole:
return QVariant::fromValue((*registry_)[index.row()].second->icon());
}
} else {
switch(role) {
case Qt::DisplayRole:
return QVariant::fromValue((*registry_)[index.row()].second->shortcut().toString());
}
}
return QVariant();
}
QVariant ShortcutTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
switch (role)
{
case Qt::DisplayRole:
if (orientation == Qt::Vertical)
{
return QVariant();
}
if (section == 0)
{
return QVariant::fromValue(QString("Description"));
}
else
{
return QVariant::fromValue(QString("Key Sequence"));
}
default:
return QVariant();
}
}
Qt::ItemFlags ShortcutTableModel::flags(const QModelIndex& /*index*/) const
{
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}
bool ShortcutTableModel::isValid(const QKeySequence& seq) const
{
return !registry_->hasKey(seq.toString());
}
bool ShortcutTableModel::setData(const QModelIndex& index, const QVariant& data, int /*role*/)
{
QKeySequence seq = qvariant_cast<QKeySequence>(data);
QString new_seq = seq.toString();
if ((index.column() == 1) && registry_->changeShortcut(index.row(), ascii(new_seq)))
{
// TODO: getDescription(QKeySequence) in shortcutRegistry to provide more helpful message.
Scene::getInstance(0)->setStatusbarText("Shortcut " + ascii(new_seq) + " successfully set.");
Q_EMIT dataChanged(index, index);
return true;
}
// TODO: getDescription(QKeySequence) in shortcutRegistry to provide more helpful message.
Scene::getInstance(0)->setStatusbarText("Shortcut " + ascii(new_seq) + " already associated!");
return false;
}
///////////////////////// ShortcutTableView ///////////////////////////////
ShortcutTableView::ShortcutTableView(QWidget* parent)
: QTableView(parent), edited_row_(-1)
{
// Create a new model
ShortcutTableModel* model = new ShortcutTableModel(ShortcutRegistry::getInstance(0));
proxy_model_ = new QSortFilterProxyModel(this);
proxy_model_->setDynamicSortFilter(true);
proxy_model_->setSourceModel(model);
proxy_model_->setFilterCaseSensitivity(Qt::CaseInsensitive);
// Setting up a view to display the items in the model
setModel(proxy_model_);
editor_ = new EditSingleShortcut(this);
connect(editor_, SIGNAL(accepted()), this, SLOT(editSuccess_()));
connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
this, SLOT( onClick(const QModelIndex&)));
connect(this, SIGNAL(clicked(const QModelIndex&)),
this, SLOT( onClick(const QModelIndex&)));
connect(ShortcutRegistry::getInstance(0), SIGNAL(shortcutChanged()),
proxy_model_, SLOT(invalidate()));
resizeColumnsToContents();
}
ShortcutTableView::~ShortcutTableView()
{
delete proxy_model_->sourceModel();
}
void ShortcutTableView::onClick(const QModelIndex& index)
{
if(!index.isValid() || editor_->isVisible()) {
return;
}
QModelIndex sc_index = index.model()->index(index.row(), 1, QModelIndex());
edited_row_ = sc_index.row();
editor_->setup(sc_index.data().toString());
editor_->show();
}
void ShortcutTableView::setFilter(const QString& filter)
{
proxy_model_->setFilterFixedString(filter);
}
void ShortcutTableView::editSuccess_()
{
if(edited_row_ < 0) {
return;
}
if(model()->setData(model()->index(edited_row_, 1, QModelIndex()),
QVariant::fromValue(editor_->getKeySequence()))) {
Q_EMIT shortcutChanged();
}
}
}
}
|