diff --git a/api/logic/InstanceList.cpp b/api/logic/InstanceList.cpp index ed4286e3..e568211b 100644 --- a/api/logic/InstanceList.cpp +++ b/api/logic/InstanceList.cpp @@ -95,6 +95,7 @@ QVariant InstanceList::data(const QModelIndex &index, int role) const { return pdata->id(); } + case Qt::EditRole: case Qt::DisplayRole: { return pdata->name(); @@ -118,12 +119,32 @@ QVariant InstanceList::data(const QModelIndex &index, int role) const return QVariant(); } +bool InstanceList::setData(const QModelIndex& index, const QVariant& value, int role) +{ + if (!index.isValid()) + { + return false; + } + if(role != Qt::EditRole) + { + return false; + } + BaseInstance *pdata = static_cast(index.internalPointer()); + auto newName = value.toString(); + if(pdata->name() == newName) + { + return true; + } + pdata->setName(newName); + return true; +} + Qt::ItemFlags InstanceList::flags(const QModelIndex &index) const { Qt::ItemFlags f; if (index.isValid()) { - f |= (Qt::ItemIsEnabled | Qt::ItemIsSelectable); + f |= (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable); } return f; } diff --git a/api/logic/InstanceList.h b/api/logic/InstanceList.h index 5b966b33..5c172e47 100644 --- a/api/logic/InstanceList.h +++ b/api/logic/InstanceList.h @@ -58,10 +58,12 @@ public: virtual ~InstanceList(); public: - QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const; - int rowCount(const QModelIndex &parent = QModelIndex()) const; - QVariant data(const QModelIndex &index, int role) const; - Qt::ItemFlags flags(const QModelIndex &index) const; + QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override; + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role) const override; + Qt::ItemFlags flags(const QModelIndex &index) const override; + + bool setData(const QModelIndex & index, const QVariant & value, int role) override; enum AdditionalRoles { diff --git a/application/MainWindow.cpp b/application/MainWindow.cpp index 595e0615..66e1f141 100644 --- a/application/MainWindow.cpp +++ b/application/MainWindow.cpp @@ -1091,10 +1091,12 @@ bool MainWindow::eventFilter(QObject *obj, QEvent *ev) QKeyEvent *keyEvent = static_cast(ev); switch (keyEvent->key()) { + /* case Qt::Key_Enter: case Qt::Key_Return: activateInstance(m_selectedInstance); return true; + */ case Qt::Key_Delete: on_actionDeleteInstance_triggered(); return true; @@ -1639,19 +1641,7 @@ void MainWindow::on_actionRenameInstance_triggered() { if (m_selectedInstance) { - bool ok = false; - QString name(m_selectedInstance->name()); - name = QInputDialog::getText(this, tr("Instance name"), tr("Enter a new instance name."), QLineEdit::Normal, name, &ok); - - name = name.trimmed(); - if (name.length() > 0) - { - if (ok && name.length()) - { - m_selectedInstance->setName(name); - ui->renameButton->setText(name); - } - } + view->edit(view->currentIndex()); } } diff --git a/application/groupview/InstanceDelegate.cpp b/application/groupview/InstanceDelegate.cpp index 42860aef..75ce6fc3 100644 --- a/application/groupview/InstanceDelegate.cpp +++ b/application/groupview/InstanceDelegate.cpp @@ -19,11 +19,13 @@ #include #include #include +#include #include "GroupView.h" #include "BaseInstance.h" #include "InstanceList.h" #include +#include // Origin: Qt static void viewItemTextLayout(QTextLayout &textLayout, int lineWidth, qreal &height, @@ -165,8 +167,7 @@ static QSize viewItemTextSize(const QStyleOptionViewItem *option) textLayout.setTextOption(textOption); textLayout.setFont(option->font); textLayout.setText(option->text); - const int textMargin = - style->pixelMetric(QStyle::PM_FocusFrameHMargin, option, option->widget) + 1; + const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, option, option->widget) + 1; QRect bounds(0, 0, 100 - 2 * textMargin, 600); qreal height = 0, widthUsed = 0; viewItemTextLayout(textLayout, bounds.width(), height, widthUsed); @@ -331,8 +332,7 @@ QSize ListViewDelegate::sizeHint(const QStyleOptionViewItem &option, opt.displayAlignment = Qt::AlignTop | Qt::AlignHCenter; QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); - const int textMargin = - style->pixelMetric(QStyle::PM_FocusFrameHMargin, &option, opt.widget) + 1; + const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, &option, opt.widget) + 1; int height = 48 + textMargin * 2 + 5; // TODO: turn constants into variables QSize szz = viewItemTextSize(&opt); height += szz.height(); @@ -341,3 +341,17 @@ QSize ListViewDelegate::sizeHint(const QStyleOptionViewItem &option, return sz; } +void ListViewDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + const int iconSize = 48; + QRect textRect = option.rect; + textRect.adjust(0, iconSize + 5, 0, 0); + editor->setGeometry(textRect); +} + +QWidget * ListViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + auto *le = new QLineEdit(parent); + le->setFrame(false); + return le; +} diff --git a/application/groupview/InstanceDelegate.h b/application/groupview/InstanceDelegate.h index d0076e60..251ed875 100644 --- a/application/groupview/InstanceDelegate.h +++ b/application/groupview/InstanceDelegate.h @@ -20,11 +20,18 @@ class ListViewDelegate : public QStyledItemDelegate { + Q_OBJECT + public: explicit ListViewDelegate(QObject *parent = 0); + virtual ~ListViewDelegate() {} -protected: - void paint(QPainter *painter, const QStyleOptionViewItem &option, - const QModelIndex &index) const; - QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; + void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; + QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override; + void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const override; + QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const override; + /* + void setEditorData(QWidget * editor, const QModelIndex & index) const override; + void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const override; + */ };