GH-2101 POC for inline renaming

This commit is contained in:
Petr Mrázek 2018-11-22 01:50:32 +01:00
parent c4a472981f
commit c214c13fb3
5 changed files with 60 additions and 26 deletions

View File

@ -95,6 +95,7 @@ QVariant InstanceList::data(const QModelIndex &index, int role) const
{ {
return pdata->id(); return pdata->id();
} }
case Qt::EditRole:
case Qt::DisplayRole: case Qt::DisplayRole:
{ {
return pdata->name(); return pdata->name();
@ -118,12 +119,32 @@ QVariant InstanceList::data(const QModelIndex &index, int role) const
return QVariant(); 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<BaseInstance *>(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 InstanceList::flags(const QModelIndex &index) const
{ {
Qt::ItemFlags f; Qt::ItemFlags f;
if (index.isValid()) if (index.isValid())
{ {
f |= (Qt::ItemIsEnabled | Qt::ItemIsSelectable); f |= (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
} }
return f; return f;
} }

View File

@ -58,10 +58,12 @@ public:
virtual ~InstanceList(); virtual ~InstanceList();
public: public:
QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const; QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const; int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role) const; QVariant data(const QModelIndex &index, int role) const override;
Qt::ItemFlags flags(const QModelIndex &index) const; Qt::ItemFlags flags(const QModelIndex &index) const override;
bool setData(const QModelIndex & index, const QVariant & value, int role) override;
enum AdditionalRoles enum AdditionalRoles
{ {

View File

@ -1091,10 +1091,12 @@ bool MainWindow::eventFilter(QObject *obj, QEvent *ev)
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev); QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
switch (keyEvent->key()) switch (keyEvent->key())
{ {
/*
case Qt::Key_Enter: case Qt::Key_Enter:
case Qt::Key_Return: case Qt::Key_Return:
activateInstance(m_selectedInstance); activateInstance(m_selectedInstance);
return true; return true;
*/
case Qt::Key_Delete: case Qt::Key_Delete:
on_actionDeleteInstance_triggered(); on_actionDeleteInstance_triggered();
return true; return true;
@ -1639,19 +1641,7 @@ void MainWindow::on_actionRenameInstance_triggered()
{ {
if (m_selectedInstance) if (m_selectedInstance)
{ {
bool ok = false; view->edit(view->currentIndex());
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 File

@ -19,11 +19,13 @@
#include <QTextLayout> #include <QTextLayout>
#include <QApplication> #include <QApplication>
#include <QtMath> #include <QtMath>
#include <QDebug>
#include "GroupView.h" #include "GroupView.h"
#include "BaseInstance.h" #include "BaseInstance.h"
#include "InstanceList.h" #include "InstanceList.h"
#include <xdgicon.h> #include <xdgicon.h>
#include <QPlainTextEdit>
// Origin: Qt // Origin: Qt
static void viewItemTextLayout(QTextLayout &textLayout, int lineWidth, qreal &height, static void viewItemTextLayout(QTextLayout &textLayout, int lineWidth, qreal &height,
@ -165,8 +167,7 @@ static QSize viewItemTextSize(const QStyleOptionViewItem *option)
textLayout.setTextOption(textOption); textLayout.setTextOption(textOption);
textLayout.setFont(option->font); textLayout.setFont(option->font);
textLayout.setText(option->text); textLayout.setText(option->text);
const int textMargin = const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, option, option->widget) + 1;
style->pixelMetric(QStyle::PM_FocusFrameHMargin, option, option->widget) + 1;
QRect bounds(0, 0, 100 - 2 * textMargin, 600); QRect bounds(0, 0, 100 - 2 * textMargin, 600);
qreal height = 0, widthUsed = 0; qreal height = 0, widthUsed = 0;
viewItemTextLayout(textLayout, bounds.width(), height, widthUsed); viewItemTextLayout(textLayout, bounds.width(), height, widthUsed);
@ -331,8 +332,7 @@ QSize ListViewDelegate::sizeHint(const QStyleOptionViewItem &option,
opt.displayAlignment = Qt::AlignTop | Qt::AlignHCenter; opt.displayAlignment = Qt::AlignTop | Qt::AlignHCenter;
QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
const int textMargin = const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, &option, opt.widget) + 1;
style->pixelMetric(QStyle::PM_FocusFrameHMargin, &option, opt.widget) + 1;
int height = 48 + textMargin * 2 + 5; // TODO: turn constants into variables int height = 48 + textMargin * 2 + 5; // TODO: turn constants into variables
QSize szz = viewItemTextSize(&opt); QSize szz = viewItemTextSize(&opt);
height += szz.height(); height += szz.height();
@ -341,3 +341,17 @@ QSize ListViewDelegate::sizeHint(const QStyleOptionViewItem &option,
return sz; 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;
}

View File

@ -20,11 +20,18 @@
class ListViewDelegate : public QStyledItemDelegate class ListViewDelegate : public QStyledItemDelegate
{ {
Q_OBJECT
public: public:
explicit ListViewDelegate(QObject *parent = 0); explicit ListViewDelegate(QObject *parent = 0);
virtual ~ListViewDelegate() {}
protected: void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void paint(QPainter *painter, const QStyleOptionViewItem &option, QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
const QModelIndex &index) const; void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const override;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; 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;
*/
}; };