2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-08-16 10:45:23 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-08-16 10:45:23 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-08-16 10:45:23 +02: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.
|
2011-08-16 10:45:23 +02: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.
|
2011-08-16 10:45:23 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
#include "codestyleselectorwidget.h"
|
2011-10-26 10:03:56 +02:00
|
|
|
#include "ui_codestyleselectorwidget.h"
|
2011-08-16 10:45:23 +02:00
|
|
|
#include "icodestylepreferences.h"
|
|
|
|
|
#include "icodestylepreferencesfactory.h"
|
|
|
|
|
#include "codestylepool.h"
|
|
|
|
|
#include "tabsettings.h"
|
|
|
|
|
|
2015-02-26 13:22:35 +01:00
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QPushButton>
|
|
|
|
|
#include <QDialogButtonBox>
|
|
|
|
|
#include <QDialog>
|
|
|
|
|
#include <QInputDialog>
|
|
|
|
|
#include <QLabel>
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QFileDialog>
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
|
|
using namespace TextEditor;
|
2021-08-17 16:36:42 +02:00
|
|
|
using namespace Utils;
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
namespace TextEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
class CodeStyleDialog : public QDialog
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
CodeStyleDialog(ICodeStylePreferencesFactory *factory,
|
2018-09-20 01:16:01 +03:00
|
|
|
ICodeStylePreferences *codeStyle, QWidget *parent = nullptr);
|
|
|
|
|
~CodeStyleDialog() override;
|
2011-08-16 10:45:23 +02:00
|
|
|
ICodeStylePreferences *codeStyle() const;
|
2015-12-13 01:18:33 +02:00
|
|
|
private:
|
2011-10-26 10:03:56 +02:00
|
|
|
void slotCopyClicked();
|
|
|
|
|
void slotDisplayNameChanged();
|
2015-12-13 01:18:33 +02:00
|
|
|
|
2011-08-16 10:45:23 +02:00
|
|
|
ICodeStylePreferences *m_codeStyle;
|
|
|
|
|
QLineEdit *m_lineEdit;
|
2011-10-26 10:03:56 +02:00
|
|
|
QDialogButtonBox *m_buttons;
|
2018-09-20 01:16:01 +03:00
|
|
|
QLabel *m_warningLabel = nullptr;
|
|
|
|
|
QPushButton *m_copyButton = nullptr;
|
2011-10-26 10:03:56 +02:00
|
|
|
QString m_originalDisplayName;
|
2011-08-16 10:45:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CodeStyleDialog::CodeStyleDialog(ICodeStylePreferencesFactory *factory,
|
|
|
|
|
ICodeStylePreferences *codeStyle, QWidget *parent)
|
2018-09-20 01:16:01 +03:00
|
|
|
: QDialog(parent)
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
|
|
|
|
setWindowTitle(tr("Edit Code Style"));
|
2018-09-20 01:16:01 +03:00
|
|
|
auto layout = new QVBoxLayout(this);
|
2011-08-16 10:45:23 +02:00
|
|
|
QLabel *label = new QLabel(tr("Code style name:"));
|
|
|
|
|
m_lineEdit = new QLineEdit(codeStyle->displayName(), this);
|
2018-09-20 01:16:01 +03:00
|
|
|
auto nameLayout = new QHBoxLayout;
|
2011-08-16 10:45:23 +02:00
|
|
|
nameLayout->addWidget(label);
|
|
|
|
|
nameLayout->addWidget(m_lineEdit);
|
|
|
|
|
layout->addLayout(nameLayout);
|
2011-10-26 10:03:56 +02:00
|
|
|
|
|
|
|
|
if (codeStyle->isReadOnly()) {
|
2018-09-20 01:16:01 +03:00
|
|
|
auto warningLayout = new QHBoxLayout;
|
2011-10-26 10:03:56 +02:00
|
|
|
m_warningLabel = new QLabel(
|
|
|
|
|
tr("You cannot save changes to a built-in code style. "
|
|
|
|
|
"Copy it first to create your own version."), this);
|
|
|
|
|
QFont font = m_warningLabel->font();
|
|
|
|
|
font.setItalic(true);
|
|
|
|
|
m_warningLabel->setFont(font);
|
|
|
|
|
m_warningLabel->setWordWrap(true);
|
|
|
|
|
m_copyButton = new QPushButton(tr("Copy Built-in Code Style"), this);
|
|
|
|
|
m_copyButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
2015-12-13 01:18:33 +02:00
|
|
|
connect(m_copyButton, &QAbstractButton::clicked, this, &CodeStyleDialog::slotCopyClicked);
|
2011-10-26 10:03:56 +02:00
|
|
|
warningLayout->addWidget(m_warningLabel);
|
|
|
|
|
warningLayout->addWidget(m_copyButton);
|
|
|
|
|
layout->addLayout(warningLayout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_originalDisplayName = codeStyle->displayName();
|
2011-08-16 10:45:23 +02:00
|
|
|
m_codeStyle = factory->createCodeStyle();
|
|
|
|
|
m_codeStyle->setTabSettings(codeStyle->tabSettings());
|
|
|
|
|
m_codeStyle->setValue(codeStyle->value());
|
2012-03-02 13:47:04 +01:00
|
|
|
m_codeStyle->setId(codeStyle->id());
|
2011-10-26 10:03:56 +02:00
|
|
|
m_codeStyle->setDisplayName(m_originalDisplayName);
|
2011-08-16 10:45:23 +02:00
|
|
|
QWidget *editor = factory->createEditor(m_codeStyle, this);
|
2011-10-26 10:03:56 +02:00
|
|
|
|
|
|
|
|
m_buttons = new QDialogButtonBox(
|
2011-08-16 10:45:23 +02:00
|
|
|
QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
|
2011-10-26 10:03:56 +02:00
|
|
|
if (codeStyle->isReadOnly()) {
|
|
|
|
|
QPushButton *okButton = m_buttons->button(QDialogButtonBox::Ok);
|
|
|
|
|
okButton->setEnabled(false);
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-16 10:45:23 +02:00
|
|
|
if (editor)
|
|
|
|
|
layout->addWidget(editor);
|
2011-10-26 10:03:56 +02:00
|
|
|
layout->addWidget(m_buttons);
|
2015-05-27 15:25:59 +03:00
|
|
|
resize(850, 600);
|
2011-10-26 10:03:56 +02:00
|
|
|
|
2015-12-13 01:18:33 +02:00
|
|
|
connect(m_lineEdit, &QLineEdit::textChanged, this, &CodeStyleDialog::slotDisplayNameChanged);
|
|
|
|
|
connect(m_buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
|
|
|
|
|
connect(m_buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ICodeStylePreferences *CodeStyleDialog::codeStyle() const
|
|
|
|
|
{
|
|
|
|
|
return m_codeStyle;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 10:03:56 +02:00
|
|
|
void CodeStyleDialog::slotCopyClicked()
|
|
|
|
|
{
|
|
|
|
|
if (m_warningLabel)
|
|
|
|
|
m_warningLabel->hide();
|
|
|
|
|
if (m_copyButton)
|
|
|
|
|
m_copyButton->hide();
|
|
|
|
|
QPushButton *okButton = m_buttons->button(QDialogButtonBox::Ok);
|
|
|
|
|
okButton->setEnabled(true);
|
|
|
|
|
if (m_lineEdit->text() == m_originalDisplayName)
|
|
|
|
|
m_lineEdit->setText(tr("%1 (Copy)").arg(m_lineEdit->text()));
|
|
|
|
|
m_lineEdit->selectAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStyleDialog::slotDisplayNameChanged()
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
2011-10-26 10:03:56 +02:00
|
|
|
m_codeStyle->setDisplayName(m_lineEdit->text());
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodeStyleDialog::~CodeStyleDialog()
|
|
|
|
|
{
|
|
|
|
|
delete m_codeStyle;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 16:36:42 +02:00
|
|
|
} // Internal
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
CodeStyleSelectorWidget::CodeStyleSelectorWidget(ICodeStylePreferencesFactory *factory, QWidget *parent) :
|
|
|
|
|
QWidget(parent),
|
|
|
|
|
m_factory(factory),
|
2018-09-20 01:16:01 +03:00
|
|
|
m_ui(new Internal::Ui::CodeStyleSelectorWidget)
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
2011-10-26 10:03:56 +02:00
|
|
|
m_ui->setupUi(this);
|
|
|
|
|
m_ui->importButton->setEnabled(false);
|
|
|
|
|
m_ui->exportButton->setEnabled(false);
|
2011-08-16 10:45:23 +02:00
|
|
|
|
2019-02-26 09:40:49 +01:00
|
|
|
connect(m_ui->delegateComboBox, QOverload<int>::of(&QComboBox::activated),
|
2015-12-13 01:18:33 +02:00
|
|
|
this, &CodeStyleSelectorWidget::slotComboBoxActivated);
|
|
|
|
|
connect(m_ui->copyButton, &QAbstractButton::clicked,
|
|
|
|
|
this, &CodeStyleSelectorWidget::slotCopyClicked);
|
|
|
|
|
connect(m_ui->editButton, &QAbstractButton::clicked,
|
|
|
|
|
this, &CodeStyleSelectorWidget::slotEditClicked);
|
|
|
|
|
connect(m_ui->removeButton, &QAbstractButton::clicked,
|
|
|
|
|
this, &CodeStyleSelectorWidget::slotRemoveClicked);
|
|
|
|
|
connect(m_ui->importButton, &QAbstractButton::clicked,
|
|
|
|
|
this, &CodeStyleSelectorWidget::slotImportClicked);
|
|
|
|
|
connect(m_ui->exportButton, &QAbstractButton::clicked,
|
|
|
|
|
this, &CodeStyleSelectorWidget::slotExportClicked);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
2011-10-26 10:03:56 +02:00
|
|
|
CodeStyleSelectorWidget::~CodeStyleSelectorWidget()
|
|
|
|
|
{
|
|
|
|
|
delete m_ui;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-03 23:46:35 +02:00
|
|
|
void CodeStyleSelectorWidget::setCodeStyle(ICodeStylePreferences *codeStyle)
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
|
|
|
|
if (m_codeStyle == codeStyle)
|
|
|
|
|
return; // nothing changes
|
|
|
|
|
|
|
|
|
|
// cleanup old
|
|
|
|
|
if (m_codeStyle) {
|
|
|
|
|
CodeStylePool *codeStylePool = m_codeStyle->delegatingPool();
|
|
|
|
|
if (codeStylePool) {
|
2015-12-13 01:18:33 +02:00
|
|
|
disconnect(codeStylePool, &CodeStylePool::codeStyleAdded,
|
|
|
|
|
this, &CodeStyleSelectorWidget::slotCodeStyleAdded);
|
|
|
|
|
disconnect(codeStylePool, &CodeStylePool::codeStyleRemoved,
|
|
|
|
|
this, &CodeStyleSelectorWidget::slotCodeStyleRemoved);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
2015-12-13 01:18:33 +02:00
|
|
|
disconnect(m_codeStyle, &ICodeStylePreferences::currentDelegateChanged,
|
|
|
|
|
this, &CodeStyleSelectorWidget::slotCurrentDelegateChanged);
|
2011-08-16 10:45:23 +02:00
|
|
|
|
2011-10-26 10:03:56 +02:00
|
|
|
m_ui->exportButton->setEnabled(false);
|
|
|
|
|
m_ui->importButton->setEnabled(false);
|
|
|
|
|
m_ui->delegateComboBox->clear();
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
m_codeStyle = codeStyle;
|
|
|
|
|
// fillup new
|
|
|
|
|
if (m_codeStyle) {
|
|
|
|
|
QList<ICodeStylePreferences *> delegates;
|
|
|
|
|
CodeStylePool *codeStylePool = m_codeStyle->delegatingPool();
|
|
|
|
|
if (codeStylePool) {
|
|
|
|
|
delegates = codeStylePool->codeStyles();
|
|
|
|
|
|
2015-12-13 01:18:33 +02:00
|
|
|
connect(codeStylePool, &CodeStylePool::codeStyleAdded,
|
|
|
|
|
this, &CodeStyleSelectorWidget::slotCodeStyleAdded);
|
|
|
|
|
connect(codeStylePool, &CodeStylePool::codeStyleRemoved,
|
|
|
|
|
this, &CodeStyleSelectorWidget::slotCodeStyleRemoved);
|
2011-10-26 10:03:56 +02:00
|
|
|
m_ui->exportButton->setEnabled(true);
|
|
|
|
|
m_ui->importButton->setEnabled(true);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < delegates.count(); i++)
|
|
|
|
|
slotCodeStyleAdded(delegates.at(i));
|
|
|
|
|
|
|
|
|
|
slotCurrentDelegateChanged(m_codeStyle->currentDelegate());
|
|
|
|
|
|
2015-12-13 01:18:33 +02:00
|
|
|
connect(m_codeStyle, &ICodeStylePreferences::currentDelegateChanged,
|
|
|
|
|
this, &CodeStyleSelectorWidget::slotCurrentDelegateChanged);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStyleSelectorWidget::slotComboBoxActivated(int index)
|
|
|
|
|
{
|
|
|
|
|
if (m_ignoreGuiSignals)
|
|
|
|
|
return;
|
|
|
|
|
|
2011-10-26 10:03:56 +02:00
|
|
|
if (index < 0 || index >= m_ui->delegateComboBox->count())
|
2011-08-16 10:45:23 +02:00
|
|
|
return;
|
2018-09-20 01:16:01 +03:00
|
|
|
auto delegate = m_ui->delegateComboBox->itemData(index).value<ICodeStylePreferences *>();
|
2011-08-16 10:45:23 +02:00
|
|
|
|
2017-09-30 07:12:57 +02:00
|
|
|
QSignalBlocker blocker(this);
|
2011-08-16 10:45:23 +02:00
|
|
|
m_codeStyle->setCurrentDelegate(delegate);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-03 23:46:35 +02:00
|
|
|
void CodeStyleSelectorWidget::slotCurrentDelegateChanged(ICodeStylePreferences *delegate)
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
|
|
|
|
m_ignoreGuiSignals = true;
|
2011-10-26 10:03:56 +02:00
|
|
|
m_ui->delegateComboBox->setCurrentIndex(m_ui->delegateComboBox->findData(QVariant::fromValue(delegate)));
|
|
|
|
|
m_ui->delegateComboBox->setToolTip(m_ui->delegateComboBox->currentText());
|
2011-08-16 10:45:23 +02:00
|
|
|
m_ignoreGuiSignals = false;
|
|
|
|
|
|
2011-10-26 10:03:56 +02:00
|
|
|
const bool removeEnabled = delegate && !delegate->isReadOnly() && !delegate->currentDelegate();
|
|
|
|
|
m_ui->removeButton->setEnabled(removeEnabled);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStyleSelectorWidget::slotCopyClicked()
|
|
|
|
|
{
|
|
|
|
|
if (!m_codeStyle)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
CodeStylePool *codeStylePool = m_codeStyle->delegatingPool();
|
|
|
|
|
ICodeStylePreferences *currentPreferences = m_codeStyle->currentPreferences();
|
|
|
|
|
bool ok = false;
|
|
|
|
|
const QString newName = QInputDialog::getText(this,
|
|
|
|
|
tr("Copy Code Style"),
|
|
|
|
|
tr("Code style name:"),
|
|
|
|
|
QLineEdit::Normal,
|
|
|
|
|
tr("%1 (Copy)").arg(currentPreferences->displayName()),
|
|
|
|
|
&ok);
|
2017-02-20 16:44:33 +01:00
|
|
|
if (!ok || newName.trimmed().isEmpty())
|
2011-08-16 10:45:23 +02:00
|
|
|
return;
|
|
|
|
|
ICodeStylePreferences *copy = codeStylePool->cloneCodeStyle(currentPreferences);
|
2012-07-26 16:46:23 +02:00
|
|
|
if (copy) {
|
|
|
|
|
copy->setDisplayName(newName);
|
2011-08-16 10:45:23 +02:00
|
|
|
m_codeStyle->setCurrentDelegate(copy);
|
2012-07-26 16:46:23 +02:00
|
|
|
}
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStyleSelectorWidget::slotEditClicked()
|
|
|
|
|
{
|
|
|
|
|
if (!m_codeStyle)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
ICodeStylePreferences *codeStyle = m_codeStyle->currentPreferences();
|
|
|
|
|
// check if it's read-only
|
|
|
|
|
|
|
|
|
|
Internal::CodeStyleDialog dialog(m_factory, codeStyle, this);
|
|
|
|
|
if (dialog.exec() == QDialog::Accepted) {
|
|
|
|
|
ICodeStylePreferences *dialogCodeStyle = dialog.codeStyle();
|
2011-10-26 10:03:56 +02:00
|
|
|
if (codeStyle->isReadOnly()) {
|
|
|
|
|
CodeStylePool *codeStylePool = m_codeStyle->delegatingPool();
|
|
|
|
|
codeStyle = codeStylePool->cloneCodeStyle(dialogCodeStyle);
|
|
|
|
|
if (codeStyle)
|
|
|
|
|
m_codeStyle->setCurrentDelegate(codeStyle);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-08-16 10:45:23 +02:00
|
|
|
codeStyle->setTabSettings(dialogCodeStyle->tabSettings());
|
|
|
|
|
codeStyle->setValue(dialogCodeStyle->value());
|
2011-10-26 10:03:56 +02:00
|
|
|
codeStyle->setDisplayName(dialogCodeStyle->displayName());
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStyleSelectorWidget::slotRemoveClicked()
|
|
|
|
|
{
|
|
|
|
|
if (!m_codeStyle)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
CodeStylePool *codeStylePool = m_codeStyle->delegatingPool();
|
|
|
|
|
ICodeStylePreferences *currentPreferences = m_codeStyle->currentPreferences();
|
|
|
|
|
|
|
|
|
|
QMessageBox messageBox(QMessageBox::Warning,
|
|
|
|
|
tr("Delete Code Style"),
|
|
|
|
|
tr("Are you sure you want to delete this code style permanently?"),
|
|
|
|
|
QMessageBox::Discard | QMessageBox::Cancel,
|
|
|
|
|
this);
|
|
|
|
|
|
|
|
|
|
// Change the text and role of the discard button
|
2018-09-20 01:16:01 +03:00
|
|
|
auto deleteButton = static_cast<QPushButton*>(messageBox.button(QMessageBox::Discard));
|
2011-08-16 10:45:23 +02:00
|
|
|
deleteButton->setText(tr("Delete"));
|
|
|
|
|
messageBox.addButton(deleteButton, QMessageBox::AcceptRole);
|
|
|
|
|
messageBox.setDefaultButton(deleteButton);
|
|
|
|
|
|
2015-12-13 01:18:33 +02:00
|
|
|
connect(deleteButton, &QAbstractButton::clicked, &messageBox, &QDialog::accept);
|
2011-08-16 10:45:23 +02:00
|
|
|
if (messageBox.exec() == QDialog::Accepted)
|
|
|
|
|
codeStylePool->removeCodeStyle(currentPreferences);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStyleSelectorWidget::slotImportClicked()
|
|
|
|
|
{
|
2021-08-17 16:36:42 +02:00
|
|
|
const FilePath fileName =
|
|
|
|
|
FileUtils::getOpenFilePath(this, tr("Import Code Style"), {},
|
|
|
|
|
tr("Code styles (*.xml);;All files (*)"));
|
2011-08-16 10:45:23 +02:00
|
|
|
if (!fileName.isEmpty()) {
|
|
|
|
|
CodeStylePool *codeStylePool = m_codeStyle->delegatingPool();
|
|
|
|
|
ICodeStylePreferences *importedStyle = codeStylePool->importCodeStyle(fileName);
|
|
|
|
|
if (importedStyle)
|
|
|
|
|
m_codeStyle->setCurrentDelegate(importedStyle);
|
|
|
|
|
else
|
|
|
|
|
QMessageBox::warning(this, tr("Import Code Style"),
|
2012-08-17 13:18:31 +02:00
|
|
|
tr("Cannot import code style from %1"), fileName.toUserOutput());
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStyleSelectorWidget::slotExportClicked()
|
|
|
|
|
{
|
|
|
|
|
ICodeStylePreferences *currentPreferences = m_codeStyle->currentPreferences();
|
2021-08-17 16:36:42 +02:00
|
|
|
const FilePath filePath = FileUtils::getSaveFilePath(this, tr("Export Code Style"),
|
|
|
|
|
FilePath::fromString(QString::fromUtf8(currentPreferences->id() + ".xml")),
|
2011-08-16 10:45:23 +02:00
|
|
|
tr("Code styles (*.xml);;All files (*)"));
|
2021-08-17 16:36:42 +02:00
|
|
|
if (!filePath.isEmpty()) {
|
2011-08-16 10:45:23 +02:00
|
|
|
CodeStylePool *codeStylePool = m_codeStyle->delegatingPool();
|
2021-08-17 16:36:42 +02:00
|
|
|
codeStylePool->exportCodeStyle(filePath, currentPreferences);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStyleSelectorWidget::slotCodeStyleAdded(ICodeStylePreferences *codeStylePreferences)
|
|
|
|
|
{
|
|
|
|
|
if (codeStylePreferences == m_codeStyle
|
|
|
|
|
|| codeStylePreferences->id() == m_codeStyle->id())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const QVariant data = QVariant::fromValue(codeStylePreferences);
|
|
|
|
|
const QString name = displayName(codeStylePreferences);
|
2011-10-26 10:03:56 +02:00
|
|
|
m_ui->delegateComboBox->addItem(name, data);
|
|
|
|
|
m_ui->delegateComboBox->setItemData(m_ui->delegateComboBox->count() - 1, name, Qt::ToolTipRole);
|
2015-12-13 01:18:33 +02:00
|
|
|
connect(codeStylePreferences, &ICodeStylePreferences::displayNameChanged,
|
|
|
|
|
this, &CodeStyleSelectorWidget::slotUpdateName);
|
2011-08-16 10:45:23 +02:00
|
|
|
if (codeStylePreferences->delegatingPool()) {
|
2015-12-13 01:18:33 +02:00
|
|
|
connect(codeStylePreferences, &ICodeStylePreferences::currentPreferencesChanged,
|
|
|
|
|
this, &CodeStyleSelectorWidget::slotUpdateName);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStyleSelectorWidget::slotCodeStyleRemoved(ICodeStylePreferences *codeStylePreferences)
|
|
|
|
|
{
|
|
|
|
|
m_ignoreGuiSignals = true;
|
2011-10-26 10:03:56 +02:00
|
|
|
m_ui->delegateComboBox->removeItem(m_ui->delegateComboBox->findData(QVariant::fromValue(codeStylePreferences)));
|
2015-12-13 01:18:33 +02:00
|
|
|
disconnect(codeStylePreferences, &ICodeStylePreferences::displayNameChanged,
|
|
|
|
|
this, &CodeStyleSelectorWidget::slotUpdateName);
|
2011-08-16 10:45:23 +02:00
|
|
|
if (codeStylePreferences->delegatingPool()) {
|
2015-12-13 01:18:33 +02:00
|
|
|
disconnect(codeStylePreferences, &ICodeStylePreferences::currentPreferencesChanged,
|
|
|
|
|
this, &CodeStyleSelectorWidget::slotUpdateName);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
m_ignoreGuiSignals = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStyleSelectorWidget::slotUpdateName()
|
|
|
|
|
{
|
2018-09-20 01:16:01 +03:00
|
|
|
auto changedCodeStyle = qobject_cast<ICodeStylePreferences *>(sender());
|
2011-08-16 10:45:23 +02:00
|
|
|
if (!changedCodeStyle)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
updateName(changedCodeStyle);
|
|
|
|
|
|
|
|
|
|
QList<ICodeStylePreferences *> codeStyles = m_codeStyle->delegatingPool()->codeStyles();
|
|
|
|
|
for (int i = 0; i < codeStyles.count(); i++) {
|
|
|
|
|
ICodeStylePreferences *codeStyle = codeStyles.at(i);
|
|
|
|
|
if (codeStyle->currentDelegate() == changedCodeStyle)
|
|
|
|
|
updateName(codeStyle);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 10:03:56 +02:00
|
|
|
m_ui->delegateComboBox->setToolTip(m_ui->delegateComboBox->currentText());
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStyleSelectorWidget::updateName(ICodeStylePreferences *codeStyle)
|
|
|
|
|
{
|
2011-10-26 10:03:56 +02:00
|
|
|
const int idx = m_ui->delegateComboBox->findData(QVariant::fromValue(codeStyle));
|
2011-08-16 10:45:23 +02:00
|
|
|
if (idx < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const QString name = displayName(codeStyle);
|
2011-10-26 10:03:56 +02:00
|
|
|
m_ui->delegateComboBox->setItemText(idx, name);
|
|
|
|
|
m_ui->delegateComboBox->setItemData(idx, name, Qt::ToolTipRole);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CodeStyleSelectorWidget::displayName(ICodeStylePreferences *codeStyle) const
|
|
|
|
|
{
|
|
|
|
|
QString name = codeStyle->displayName();
|
|
|
|
|
if (codeStyle->currentDelegate())
|
|
|
|
|
name = tr("%1 [proxy: %2]").arg(name).arg(codeStyle->currentDelegate()->displayName());
|
|
|
|
|
if (codeStyle->isReadOnly())
|
|
|
|
|
name = tr("%1 [built-in]").arg(name);
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 16:36:42 +02:00
|
|
|
} // TextEditor
|
|
|
|
|
|
2011-08-16 10:45:23 +02:00
|
|
|
#include "codestyleselectorwidget.moc"
|