1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "listmodeleditordialog.h"
27 #include "listmodeleditormodel.h"
28 
29 #include <theme.h>
30 #include <qmldesignericons.h>
31 
32 #include <coreplugin/icore.h>
33 #include <utils/algorithm.h>
34 #include <utils/stylehelper.h>
35 
36 #include <QHeaderView>
37 #include <QInputDialog>
38 #include <QKeyEvent>
39 #include <QLineEdit>
40 #include <QTableView>
41 #include <QToolBar>
42 #include <QVBoxLayout>
43 
44 #include <vector>
45 
46 namespace QmlDesigner {
47 
48 namespace {
getIcon(Theme::Icon icon)49 QIcon getIcon(Theme::Icon icon)
50 {
51     const QString fontName = "qtds_propertyIconFont.ttf";
52 
53     return Utils::StyleHelper::getIconFromIconFont(fontName, Theme::getIconUnicode(icon), 30, 30);
54 }
55 } // namespace
56 
ListModelEditorDialog(QWidget * parent)57 ListModelEditorDialog::ListModelEditorDialog(QWidget *parent)
58     : QDialog(parent)
59 {
60     resize((Core::ICore::mainWindow()->size() * 8) / 10);
61 
62     QVBoxLayout *mainLayout = new QVBoxLayout(this);
63 
64     QToolBar *toolBar = new QToolBar();
65     toolBar->setIconSize({30, 30});
66     mainLayout->addWidget(toolBar);
67     m_tableView = new QTableView;
68     mainLayout->addWidget(m_tableView);
69 
70     m_addRowAction = toolBar->addAction(getIcon(Theme::Icon::addRowAfter), tr("Add Row"));
71     m_removeRowsAction = toolBar->addAction(getIcon(Theme::Icon::deleteRow), tr("Remove Columns"));
72     m_addColumnAction = toolBar->addAction(getIcon(Theme::Icon::addColumnAfter), tr("Add Column"));
73     m_removeColumnsAction = toolBar->addAction(getIcon(Theme::Icon::deleteColumn),
74                                                tr("Remove Columns"));
75     m_moveDownAction = toolBar->addAction(Icons::ARROW_DOWN.icon(), tr("Move Down (Ctrl + Down)"));
76     m_moveDownAction->setShortcut(QKeySequence(Qt::Key_Down | Qt::CTRL));
77     m_moveUpAction = toolBar->addAction(Icons::ARROW_UP.icon(), tr("Move Up (Ctrl + Up)"));
78     m_moveDownAction->setShortcut(QKeySequence(Qt::Key_Up | Qt::CTRL));
79 }
80 
81 ListModelEditorDialog::~ListModelEditorDialog() = default;
82 
setModel(ListModelEditorModel * model)83 void ListModelEditorDialog::setModel(ListModelEditorModel *model)
84 {
85     m_model = model;
86 
87     connect(m_addRowAction, &QAction::triggered, m_model, &ListModelEditorModel::addRow);
88     connect(m_addColumnAction, &QAction::triggered, this, &ListModelEditorDialog::openColumnDialog);
89     connect(m_removeRowsAction, &QAction::triggered, this, &ListModelEditorDialog::removeRows);
90     connect(m_removeColumnsAction, &QAction::triggered, this, &ListModelEditorDialog::removeColumns);
91     connect(m_moveDownAction, &QAction::triggered, this, &ListModelEditorDialog::moveRowsDown);
92     connect(m_moveUpAction, &QAction::triggered, this, &ListModelEditorDialog::moveRowsUp);
93     connect(m_tableView->horizontalHeader(),
94             &QHeaderView::sectionDoubleClicked,
95             this,
96             &ListModelEditorDialog::changeHeader);
97 
98     m_tableView->setModel(model);
99     m_tableView->horizontalHeader()->setMinimumSectionSize(60);
100     m_tableView->verticalHeader()->setMinimumSectionSize(25);
101     m_tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
102     m_tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
103 }
104 
keyPressEvent(QKeyEvent * event)105 void ListModelEditorDialog::keyPressEvent(QKeyEvent *event)
106 {
107     if (event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
108         for (const QModelIndex index : m_tableView->selectionModel()->selectedIndexes())
109             m_model->setData(index, QVariant(), Qt::EditRole);
110     }
111 }
112 
openColumnDialog()113 void ListModelEditorDialog::openColumnDialog()
114 {
115     bool ok;
116     QString columnName = QInputDialog::getText(
117         this, tr("Add Property"), tr("Property name:"), QLineEdit::Normal, "", &ok);
118     if (ok && !columnName.isEmpty())
119         m_model->addColumn(columnName);
120 }
121 
removeRows()122 void ListModelEditorDialog::removeRows()
123 {
124     m_model->removeRows(m_tableView->selectionModel()->selectedRows());
125 }
126 
removeColumns()127 void ListModelEditorDialog::removeColumns()
128 {
129     m_model->removeColumns(m_tableView->selectionModel()->selectedColumns());
130 }
131 
changeHeader(int column)132 void ListModelEditorDialog::changeHeader(int column)
133 {
134     if (column < 0)
135         return;
136 
137     const QString propertyName = QString::fromUtf8(m_model->propertyNames()[column]);
138 
139     bool ok;
140     QString newPropertyName = QInputDialog::getText(
141         this, tr("Change Property"), tr("Column name:"), QLineEdit::Normal, propertyName, &ok);
142 
143     if (ok && !newPropertyName.isEmpty())
144         m_model->renameColumn(column, newPropertyName);
145 }
146 
moveRowsDown()147 void ListModelEditorDialog::moveRowsDown()
148 {
149     QItemSelection selection = m_model->moveRowsDown(m_tableView->selectionModel()->selectedRows());
150     m_tableView->selectionModel()->select(selection, QItemSelectionModel::Select);
151 }
152 
moveRowsUp()153 void ListModelEditorDialog::moveRowsUp()
154 {
155     QItemSelection selection = m_model->moveRowsUp(m_tableView->selectionModel()->selectedRows());
156     m_tableView->selectionModel()->select(selection, QItemSelectionModel::Select);
157 }
158 
159 } // namespace QmlDesigner
160