diff options
author | Marco Bubke <marco.bubke@qt.io> | 2020-06-18 19:46:01 +0200 |
---|---|---|
committer | Tim Jenssen <tim.jenssen@qt.io> | 2020-06-30 10:28:07 +0000 |
commit | 1e6807c680924f39c071a5d5db4a3881adb14c19 (patch) | |
tree | 5b48ff6d75678e13e6422952a8ae5aed9f0916f8 /src/plugins/qmldesigner/components/listmodeleditor/listmodeleditordialog.cpp | |
parent | 009c2745e27a9b1e9bc3886f159c271fb491c7d4 (diff) |
QmlDesigner: Add listmodeleditor
Task-number: QDS-2294
Change-Id: I66cae3a0d4265ab112eaf6b04e3a5972d185ff43
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
Diffstat (limited to 'src/plugins/qmldesigner/components/listmodeleditor/listmodeleditordialog.cpp')
-rw-r--r-- | src/plugins/qmldesigner/components/listmodeleditor/listmodeleditordialog.cpp | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/src/plugins/qmldesigner/components/listmodeleditor/listmodeleditordialog.cpp b/src/plugins/qmldesigner/components/listmodeleditor/listmodeleditordialog.cpp new file mode 100644 index 0000000000..4a8745b62d --- /dev/null +++ b/src/plugins/qmldesigner/components/listmodeleditor/listmodeleditordialog.cpp @@ -0,0 +1,163 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#include "listmodeleditordialog.h" +#include "listmodeleditormodel.h" + +#include <theme.h> + +#include <coreplugin/icore.h> +#include <utils/algorithm.h> +#include <utils/stylehelper.h> + +#include <QHeaderView> +#include <QInputDialog> +#include <QKeyEvent> +#include <QTableView> +#include <QToolBar> + +#include <vector> + +namespace QmlDesigner { + +namespace { +QIcon getIcon(Theme::Icon icon) +{ + const QString fontName = "qtds_propertyIconFont.ttf"; + + return Utils::StyleHelper::getIconFromIconFont(fontName, Theme::getIconUnicode(icon), 30, 30); +} +} // namespace + +ListModelEditorDialog::ListModelEditorDialog(QWidget *parent) + : QDialog(parent) +{ + resize((Core::ICore::mainWindow()->size() * 8) / 10); + + QVBoxLayout *mainLayout = new QVBoxLayout(this); + + QToolBar *toolBar = new QToolBar(); + toolBar->setIconSize({30, 30}); + mainLayout->addWidget(toolBar); + m_tableView = new QTableView; + mainLayout->addWidget(m_tableView); + + m_addRowAction = toolBar->addAction(getIcon(Theme::Icon::addRowAfter), tr("Add Row")); + m_removeRowsAction = toolBar->addAction(getIcon(Theme::Icon::deleteRow), tr("Remove Columns")); + m_addColumnAction = toolBar->addAction(getIcon(Theme::Icon::addColumnAfter), tr("Add Column")); + m_removeColumnsAction = toolBar->addAction(getIcon(Theme::Icon::deleteColumn), + tr("Remove Columns")); +} + +ListModelEditorDialog::~ListModelEditorDialog() = default; + +void ListModelEditorDialog::setModel(ListModelEditorModel *model) +{ + m_model = model; + + connect(m_addRowAction, &QAction::triggered, m_model, &ListModelEditorModel::addRow); + connect(m_addColumnAction, &QAction::triggered, this, &ListModelEditorDialog::openColumnDialog); + connect(m_removeRowsAction, &QAction::triggered, this, &ListModelEditorDialog::removeRows); + connect(m_removeColumnsAction, &QAction::triggered, this, &ListModelEditorDialog::removeColumns); + connect(m_tableView->horizontalHeader(), + &QHeaderView::sectionDoubleClicked, + this, + &ListModelEditorDialog::changeHeader); + + m_tableView->setModel(model); + m_tableView->horizontalHeader()->setMinimumSectionSize(60); + m_tableView->verticalHeader()->setMinimumSectionSize(25); + m_tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); + m_tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); +} + +void ListModelEditorDialog::keyPressEvent(QKeyEvent *event) +{ + if (event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) { + for (const QModelIndex index : m_tableView->selectionModel()->selectedIndexes()) + m_model->setData(index, QVariant(), Qt::EditRole); + } +} + +void ListModelEditorDialog::openColumnDialog() +{ + bool ok; + QString columnName = QInputDialog::getText( + this, tr("Add Property"), tr("Property Name:"), QLineEdit::Normal, "", &ok); + if (ok && !columnName.isEmpty()) + m_model->addColumn(columnName); +} + +void ListModelEditorDialog::removeRows() +{ + const QList<QModelIndex> indices = m_tableView->selectionModel()->selectedRows(); + std::vector<int> rows; + rows.reserve(indices.size()); + + for (QModelIndex index : indices) + rows.push_back(index.row()); + + std::sort(rows.begin(), rows.end()); + + rows.erase(std::unique(rows.begin(), rows.end()), rows.end()); + + std::reverse(rows.begin(), rows.end()); + + for (int row : rows) + m_model->removeRow(row); +} + +void ListModelEditorDialog::removeColumns() +{ + const QList<QModelIndex> indices = m_tableView->selectionModel()->selectedColumns(); + std::vector<int> columns; + columns.reserve(indices.size()); + + for (QModelIndex index : indices) + columns.push_back(index.column()); + + std::sort(columns.begin(), columns.end()); + + columns.erase(std::unique(columns.begin(), columns.end()), columns.end()); + + std::reverse(columns.begin(), columns.end()); + + for (int row : columns) + m_model->removeColumn(row); +} + +void ListModelEditorDialog::changeHeader(int column) +{ + const QString propertyName = QString::fromUtf8(m_model->propertyNames()[column]); + + bool ok; + QString newPropertyName = QInputDialog::getText( + this, tr("Change Propertry"), tr("Column Name:"), QLineEdit::Normal, propertyName, &ok); + + if (ok && !newPropertyName.isEmpty()) + m_model->renameColumn(column, newPropertyName); +} + +} // namespace QmlDesigner |