2016-01-15 14:57:40 +01:00
|
|
|
/****************************************************************************
|
2010-12-20 10:35:30 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 Nicolas Arnaud-Cormos
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-12-20 10:35:30 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-12-20 10:35:30 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2010-12-20 10:35:30 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2010-12-20 11:10:30 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-12-20 10:35:30 +01:00
|
|
|
|
|
|
|
|
#include "macrooptionswidget.h"
|
2022-07-20 17:30:47 +02:00
|
|
|
|
2010-12-20 10:35:30 +01:00
|
|
|
#include "macrosconstants.h"
|
|
|
|
|
#include "macromanager.h"
|
|
|
|
|
#include "macro.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/icore.h>
|
2011-02-07 11:34:02 +01:00
|
|
|
#include <coreplugin/coreconstants.h>
|
2010-12-20 10:35:30 +01:00
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
|
|
|
|
#include <coreplugin/actionmanager/command.h>
|
|
|
|
|
|
2022-07-20 17:30:47 +02:00
|
|
|
#include <utils/layoutbuilder.h>
|
|
|
|
|
|
2018-01-09 13:42:06 +01:00
|
|
|
#include <QAction>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QFileInfo>
|
2022-07-20 17:30:47 +02:00
|
|
|
#include <QGroupBox>
|
|
|
|
|
#include <QHeaderView>
|
|
|
|
|
#include <QLabel>
|
|
|
|
|
#include <QLineEdit>
|
|
|
|
|
#include <QPushButton>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTreeWidget>
|
|
|
|
|
#include <QTreeWidgetItem>
|
2010-12-20 10:35:30 +01:00
|
|
|
|
2022-07-20 17:30:47 +02:00
|
|
|
namespace Macros::Internal {
|
2010-12-20 10:35:30 +01:00
|
|
|
|
2022-07-20 17:30:47 +02:00
|
|
|
const int NAME_ROLE = Qt::UserRole;
|
|
|
|
|
const int WRITE_ROLE = Qt::UserRole + 1;
|
2010-12-20 10:35:30 +01:00
|
|
|
|
2022-07-20 17:30:47 +02:00
|
|
|
MacroOptionsWidget::MacroOptionsWidget()
|
2010-12-20 10:35:30 +01:00
|
|
|
{
|
2022-07-20 17:30:47 +02:00
|
|
|
m_treeWidget = new QTreeWidget;
|
|
|
|
|
m_treeWidget->setTextElideMode(Qt::ElideLeft);
|
|
|
|
|
m_treeWidget->setUniformRowHeights(true);
|
|
|
|
|
m_treeWidget->setSortingEnabled(true);
|
|
|
|
|
m_treeWidget->setColumnCount(3);
|
|
|
|
|
m_treeWidget->header()->setSortIndicatorShown(true);
|
|
|
|
|
m_treeWidget->header()->setStretchLastSection(true);
|
|
|
|
|
m_treeWidget->header()->setSortIndicator(0, Qt::AscendingOrder);
|
|
|
|
|
m_treeWidget->setHeaderLabels({tr("Name"), tr("Description)"), tr("Shortcut")});
|
|
|
|
|
|
|
|
|
|
m_description = new QLineEdit;
|
|
|
|
|
|
|
|
|
|
m_removeButton = new QPushButton(tr("Remove"));
|
|
|
|
|
|
|
|
|
|
m_macroGroup = new QGroupBox(tr("Macro"), this);
|
|
|
|
|
|
|
|
|
|
using namespace Utils::Layouting;
|
|
|
|
|
|
|
|
|
|
Row {
|
|
|
|
|
tr("Description:"), m_description
|
|
|
|
|
}.attachTo(m_macroGroup);
|
|
|
|
|
|
|
|
|
|
Column {
|
|
|
|
|
Group {
|
|
|
|
|
Title(tr("Preferences")),
|
|
|
|
|
Row {
|
|
|
|
|
m_treeWidget,
|
|
|
|
|
Column { m_removeButton, Stretch() },
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
m_macroGroup
|
|
|
|
|
}.attachTo(this);
|
2010-12-20 10:35:30 +01:00
|
|
|
|
2022-07-20 17:30:47 +02:00
|
|
|
connect(m_treeWidget, &QTreeWidget::currentItemChanged,
|
2015-01-29 14:32:36 +01:00
|
|
|
this, &MacroOptionsWidget::changeCurrentItem);
|
2022-07-20 17:30:47 +02:00
|
|
|
connect(m_removeButton, &QPushButton::clicked,
|
2015-01-29 14:32:36 +01:00
|
|
|
this, &MacroOptionsWidget::remove);
|
2022-07-20 17:30:47 +02:00
|
|
|
connect(m_description, &QLineEdit::textChanged,
|
2015-01-29 14:32:36 +01:00
|
|
|
this, &MacroOptionsWidget::changeDescription);
|
2010-12-20 10:35:30 +01:00
|
|
|
|
2011-02-07 11:34:02 +01:00
|
|
|
initialize();
|
2010-12-20 10:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
2022-07-20 17:30:47 +02:00
|
|
|
MacroOptionsWidget::~MacroOptionsWidget() = default;
|
2010-12-20 10:35:30 +01:00
|
|
|
|
2011-02-07 11:34:02 +01:00
|
|
|
void MacroOptionsWidget::initialize()
|
2010-12-20 10:35:30 +01:00
|
|
|
{
|
|
|
|
|
m_macroToRemove.clear();
|
|
|
|
|
m_macroToChange.clear();
|
2022-07-20 17:30:47 +02:00
|
|
|
m_treeWidget->clear();
|
|
|
|
|
changeCurrentItem(nullptr);
|
2010-12-20 10:35:30 +01:00
|
|
|
|
|
|
|
|
// Create the treeview
|
2011-02-07 11:34:02 +01:00
|
|
|
createTable();
|
2010-12-20 10:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
2011-02-07 11:34:02 +01:00
|
|
|
void MacroOptionsWidget::createTable()
|
2010-12-20 10:35:30 +01:00
|
|
|
{
|
2013-08-02 12:33:17 +02:00
|
|
|
QDir dir(MacroManager::macrosDirectory());
|
2020-06-26 13:59:38 +02:00
|
|
|
const Utils::Id base = Utils::Id(Constants::PREFIX_MACRO);
|
2019-07-24 13:43:54 +02:00
|
|
|
for (Macro *macro : MacroManager::macros()) {
|
2014-05-19 18:34:12 +03:00
|
|
|
QFileInfo fileInfo(macro->fileName());
|
2010-12-20 10:35:30 +01:00
|
|
|
if (fileInfo.absoluteDir() == dir.absolutePath()) {
|
2022-07-20 17:30:47 +02:00
|
|
|
auto macroItem = new QTreeWidgetItem(m_treeWidget);
|
2014-05-19 18:34:12 +03:00
|
|
|
macroItem->setText(0, macro->displayName());
|
|
|
|
|
macroItem->setText(1, macro->description());
|
|
|
|
|
macroItem->setData(0, NAME_ROLE, macro->displayName());
|
|
|
|
|
macroItem->setData(0, WRITE_ROLE, macro->isWritable());
|
2011-01-24 11:39:42 +01:00
|
|
|
|
2012-05-24 13:49:06 +02:00
|
|
|
Core::Command *command =
|
2014-05-19 18:34:12 +03:00
|
|
|
Core::ActionManager::command(base.withSuffix(macro->displayName()));
|
2022-05-16 14:51:24 +02:00
|
|
|
if (command && command->action()) {
|
|
|
|
|
macroItem->setText(2,
|
|
|
|
|
command->action()->shortcut().toString(QKeySequence::NativeText));
|
|
|
|
|
}
|
2010-12-20 10:35:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MacroOptionsWidget::changeCurrentItem(QTreeWidgetItem *current)
|
|
|
|
|
{
|
2011-02-07 11:34:02 +01:00
|
|
|
m_changingCurrent = true;
|
2022-07-20 17:30:47 +02:00
|
|
|
m_removeButton->setEnabled(current);
|
|
|
|
|
m_macroGroup->setEnabled(current);
|
2010-12-20 10:35:30 +01:00
|
|
|
if (!current) {
|
2022-07-20 17:30:47 +02:00
|
|
|
m_description->clear();
|
2010-12-20 10:35:30 +01:00
|
|
|
} else {
|
2022-07-20 17:30:47 +02:00
|
|
|
m_description->setText(current->text(1));
|
|
|
|
|
m_description->setEnabled(current->data(0, WRITE_ROLE).toBool());
|
2010-12-20 10:35:30 +01:00
|
|
|
}
|
2011-02-07 11:34:02 +01:00
|
|
|
m_changingCurrent = false;
|
2010-12-20 10:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MacroOptionsWidget::remove()
|
|
|
|
|
{
|
2022-07-20 17:30:47 +02:00
|
|
|
QTreeWidgetItem *current = m_treeWidget->currentItem();
|
2011-02-07 11:34:02 +01:00
|
|
|
m_macroToRemove.append(current->data(0, NAME_ROLE).toString());
|
2010-12-20 10:35:30 +01:00
|
|
|
delete current;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MacroOptionsWidget::apply()
|
|
|
|
|
{
|
|
|
|
|
// Remove macro
|
2022-06-02 14:23:03 +02:00
|
|
|
for (const QString &name : qAsConst(m_macroToRemove)) {
|
2010-12-20 10:35:30 +01:00
|
|
|
MacroManager::instance()->deleteMacro(name);
|
2011-02-07 11:34:02 +01:00
|
|
|
m_macroToChange.remove(name);
|
|
|
|
|
}
|
2010-12-20 10:35:30 +01:00
|
|
|
|
|
|
|
|
// Change macro
|
2019-07-24 13:43:54 +02:00
|
|
|
for (auto it = m_macroToChange.cbegin(), end = m_macroToChange.cend(); it != end; ++it)
|
2011-02-07 11:34:02 +01:00
|
|
|
MacroManager::instance()->changeMacro(it.key(), it.value());
|
2010-12-20 10:35:30 +01:00
|
|
|
|
|
|
|
|
// Reinitialize the page
|
2011-02-07 11:34:02 +01:00
|
|
|
initialize();
|
2010-12-20 10:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MacroOptionsWidget::changeDescription(const QString &description)
|
|
|
|
|
{
|
2022-07-20 17:30:47 +02:00
|
|
|
QTreeWidgetItem *current = m_treeWidget->currentItem();
|
2011-02-07 11:34:02 +01:00
|
|
|
if (m_changingCurrent || !current)
|
2010-12-20 10:35:30 +01:00
|
|
|
return;
|
2011-02-07 11:34:02 +01:00
|
|
|
|
|
|
|
|
QString macroName = current->data(0, NAME_ROLE).toString();
|
|
|
|
|
m_macroToChange[macroName] = description;
|
|
|
|
|
current->setText(1, description);
|
|
|
|
|
QFont font = current->font(1);
|
|
|
|
|
font.setItalic(true);
|
|
|
|
|
current->setFont(1, font);
|
2010-12-20 10:35:30 +01:00
|
|
|
}
|
2022-07-20 17:30:47 +02:00
|
|
|
|
|
|
|
|
} // Macros::Internal
|