Code style selector improvements

Change layout in code style selector.
Make it possible to see the values
of built-in code style without a need to copy it.
Built-in code style can be also modified after
being copied inside code style editor.

Task-number: QTCREATORBUG-6341
Change-Id: Ifcbf807d5730ccf9026e86572710d0cc0ccf9f1a
Reviewed-by: Leandro T. C. Melo <leandro.melo@nokia.com>
Reviewed-by: Christian Kamm <christian.d.kamm@nokia.com>
This commit is contained in:
Jarek Kobus
2011-10-26 10:03:56 +02:00
committed by Christian Kamm
parent 69e02f4fb8
commit c9ea9ad58b
5 changed files with 199 additions and 71 deletions

View File

@@ -69,6 +69,9 @@ CodeStyleEditor::CodeStyleEditor(ICodeStylePreferencesFactory *factory,
tr("Edit preview contents to see how the current settings " tr("Edit preview contents to see how the current settings "
"are applied to custom code snippets. Changes in the preview " "are applied to custom code snippets. Changes in the preview "
"do not affect the current settings."), this); "do not affect the current settings."), this);
QFont font = label->font();
font.setItalic(true);
label->setFont(font);
label->setWordWrap(true); label->setWordWrap(true);
m_layout->addWidget(selector); m_layout->addWidget(selector);
m_layout->addWidget(m_preview); m_layout->addWidget(m_preview);

View File

@@ -31,6 +31,7 @@
**************************************************************************/ **************************************************************************/
#include "codestyleselectorwidget.h" #include "codestyleselectorwidget.h"
#include "ui_codestyleselectorwidget.h"
#include "icodestylepreferences.h" #include "icodestylepreferences.h"
#include "icodestylepreferencesfactory.h" #include "icodestylepreferencesfactory.h"
#include "codestylepool.h" #include "codestylepool.h"
@@ -67,15 +68,23 @@ public:
ICodeStylePreferences *codeStyle, QWidget *parent = 0); ICodeStylePreferences *codeStyle, QWidget *parent = 0);
~CodeStyleDialog(); ~CodeStyleDialog();
ICodeStylePreferences *codeStyle() const; ICodeStylePreferences *codeStyle() const;
QString displayName() const; private slots:
void slotCopyClicked();
void slotDisplayNameChanged();
private: private:
ICodeStylePreferences *m_codeStyle; ICodeStylePreferences *m_codeStyle;
QLineEdit *m_lineEdit; QLineEdit *m_lineEdit;
QDialogButtonBox *m_buttons;
QLabel *m_warningLabel;
QPushButton *m_copyButton;
QString m_originalDisplayName;
}; };
CodeStyleDialog::CodeStyleDialog(ICodeStylePreferencesFactory *factory, CodeStyleDialog::CodeStyleDialog(ICodeStylePreferencesFactory *factory,
ICodeStylePreferences *codeStyle, QWidget *parent) ICodeStylePreferences *codeStyle, QWidget *parent)
: QDialog(parent) : QDialog(parent),
m_warningLabel(0),
m_copyButton(0)
{ {
setWindowTitle(tr("Edit Code Style")); setWindowTitle(tr("Edit Code Style"));
QVBoxLayout *layout = new QVBoxLayout(this); QVBoxLayout *layout = new QVBoxLayout(this);
@@ -85,17 +94,46 @@ CodeStyleDialog::CodeStyleDialog(ICodeStylePreferencesFactory *factory,
nameLayout->addWidget(label); nameLayout->addWidget(label);
nameLayout->addWidget(m_lineEdit); nameLayout->addWidget(m_lineEdit);
layout->addLayout(nameLayout); layout->addLayout(nameLayout);
if (codeStyle->isReadOnly()) {
QHBoxLayout *warningLayout = new QHBoxLayout();
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);
connect(m_copyButton, SIGNAL(clicked()),
this, SLOT(slotCopyClicked()));
warningLayout->addWidget(m_warningLabel);
warningLayout->addWidget(m_copyButton);
layout->addLayout(warningLayout);
}
m_originalDisplayName = codeStyle->displayName();
m_codeStyle = factory->createCodeStyle(); m_codeStyle = factory->createCodeStyle();
m_codeStyle->setTabSettings(codeStyle->tabSettings()); m_codeStyle->setTabSettings(codeStyle->tabSettings());
m_codeStyle->setValue(codeStyle->value()); m_codeStyle->setValue(codeStyle->value());
m_codeStyle->setDisplayName(m_originalDisplayName);
QWidget *editor = factory->createEditor(m_codeStyle, this); QWidget *editor = factory->createEditor(m_codeStyle, this);
QDialogButtonBox *buttons = new QDialogButtonBox(
m_buttons = new QDialogButtonBox(
QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this); QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
if (codeStyle->isReadOnly()) {
QPushButton *okButton = m_buttons->button(QDialogButtonBox::Ok);
okButton->setEnabled(false);
}
if (editor) if (editor)
layout->addWidget(editor); layout->addWidget(editor);
layout->addWidget(buttons); layout->addWidget(m_buttons);
connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttons, SIGNAL(rejected()), this, SLOT(reject())); connect(m_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotDisplayNameChanged()));
connect(m_buttons, SIGNAL(accepted()), this, SLOT(accept()));
connect(m_buttons, SIGNAL(rejected()), this, SLOT(reject()));
} }
ICodeStylePreferences *CodeStyleDialog::codeStyle() const ICodeStylePreferences *CodeStyleDialog::codeStyle() const
@@ -103,9 +141,22 @@ ICodeStylePreferences *CodeStyleDialog::codeStyle() const
return m_codeStyle; return m_codeStyle;
} }
QString CodeStyleDialog::displayName() const void CodeStyleDialog::slotCopyClicked()
{ {
return m_lineEdit->text(); 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()
{
m_codeStyle->setDisplayName(m_lineEdit->text());
} }
CodeStyleDialog::~CodeStyleDialog() CodeStyleDialog::~CodeStyleDialog()
@@ -120,48 +171,32 @@ CodeStyleSelectorWidget::CodeStyleSelectorWidget(ICodeStylePreferencesFactory *f
QWidget(parent), QWidget(parent),
m_factory(factory), m_factory(factory),
m_codeStyle(0), m_codeStyle(0),
m_layout(0), m_ui(new Ui::CodeStyleSelectorWidget),
m_comboBox(0),
m_comboBoxLabel(0),
m_ignoreGuiSignals(false) m_ignoreGuiSignals(false)
{ {
m_layout = new QHBoxLayout(this); m_ui->setupUi(this);
m_layout->setContentsMargins(QMargins()); m_ui->importButton->setEnabled(false);
m_copyButton = new QPushButton(tr("Copy..."), this); m_ui->exportButton->setEnabled(false);
m_editButton = new QPushButton(tr("Edit..."), this);
m_removeButton = new QPushButton(tr("Remove"), this);
m_importButton = new QPushButton(tr("Import..."), this);
m_exportButton = new QPushButton(tr("Export..."), this);
m_importButton->setEnabled(false);
m_exportButton->setEnabled(false);
m_comboBoxLabel = new QLabel(tr("Current settings:"), this); connect(m_ui->delegateComboBox, SIGNAL(activated(int)),
m_comboBoxLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_layout->addWidget(m_comboBoxLabel);
m_comboBox = new QComboBox(this);
m_comboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
m_layout->addWidget(m_comboBox);
connect(m_comboBox, SIGNAL(activated(int)),
this, SLOT(slotComboBoxActivated(int))); this, SLOT(slotComboBoxActivated(int)));
connect(m_ui->copyButton, SIGNAL(clicked()),
m_layout->addWidget(m_copyButton);
m_layout->addWidget(m_editButton);
m_layout->addWidget(m_removeButton);
m_layout->addWidget(m_importButton);
m_layout->addWidget(m_exportButton);
connect(m_copyButton, SIGNAL(clicked()),
this, SLOT(slotCopyClicked())); this, SLOT(slotCopyClicked()));
connect(m_editButton, SIGNAL(clicked()), connect(m_ui->editButton, SIGNAL(clicked()),
this, SLOT(slotEditClicked())); this, SLOT(slotEditClicked()));
connect(m_removeButton, SIGNAL(clicked()), connect(m_ui->removeButton, SIGNAL(clicked()),
this, SLOT(slotRemoveClicked())); this, SLOT(slotRemoveClicked()));
connect(m_importButton, SIGNAL(clicked()), connect(m_ui->importButton, SIGNAL(clicked()),
this, SLOT(slotImportClicked())); this, SLOT(slotImportClicked()));
connect(m_exportButton, SIGNAL(clicked()), connect(m_ui->exportButton, SIGNAL(clicked()),
this, SLOT(slotExportClicked())); this, SLOT(slotExportClicked()));
} }
CodeStyleSelectorWidget::~CodeStyleSelectorWidget()
{
delete m_ui;
}
void CodeStyleSelectorWidget::setCodeStyle(TextEditor::ICodeStylePreferences *codeStyle) void CodeStyleSelectorWidget::setCodeStyle(TextEditor::ICodeStylePreferences *codeStyle)
{ {
if (m_codeStyle == codeStyle) if (m_codeStyle == codeStyle)
@@ -179,9 +214,9 @@ void CodeStyleSelectorWidget::setCodeStyle(TextEditor::ICodeStylePreferences *co
disconnect(m_codeStyle, SIGNAL(currentDelegateChanged(ICodeStylePreferences*)), disconnect(m_codeStyle, SIGNAL(currentDelegateChanged(ICodeStylePreferences*)),
this, SLOT(slotCurrentDelegateChanged(ICodeStylePreferences*))); this, SLOT(slotCurrentDelegateChanged(ICodeStylePreferences*)));
m_exportButton->setEnabled(false); m_ui->exportButton->setEnabled(false);
m_importButton->setEnabled(false); m_ui->importButton->setEnabled(false);
m_comboBox->clear(); m_ui->delegateComboBox->clear();
} }
m_codeStyle = codeStyle; m_codeStyle = codeStyle;
// fillup new // fillup new
@@ -195,8 +230,8 @@ void CodeStyleSelectorWidget::setCodeStyle(TextEditor::ICodeStylePreferences *co
this, SLOT(slotCodeStyleAdded(ICodeStylePreferences*))); this, SLOT(slotCodeStyleAdded(ICodeStylePreferences*)));
connect(codeStylePool, SIGNAL(codeStyleRemoved(ICodeStylePreferences*)), connect(codeStylePool, SIGNAL(codeStyleRemoved(ICodeStylePreferences*)),
this, SLOT(slotCodeStyleRemoved(ICodeStylePreferences*))); this, SLOT(slotCodeStyleRemoved(ICodeStylePreferences*)));
m_exportButton->setEnabled(true); m_ui->exportButton->setEnabled(true);
m_importButton->setEnabled(true); m_ui->importButton->setEnabled(true);
} }
for (int i = 0; i < delegates.count(); i++) for (int i = 0; i < delegates.count(); i++)
@@ -214,10 +249,10 @@ void CodeStyleSelectorWidget::slotComboBoxActivated(int index)
if (m_ignoreGuiSignals) if (m_ignoreGuiSignals)
return; return;
if (!m_comboBox || index < 0 || index >= m_comboBox->count()) if (index < 0 || index >= m_ui->delegateComboBox->count())
return; return;
TextEditor::ICodeStylePreferences *delegate = TextEditor::ICodeStylePreferences *delegate =
m_comboBox->itemData(index).value<TextEditor::ICodeStylePreferences *>(); m_ui->delegateComboBox->itemData(index).value<TextEditor::ICodeStylePreferences *>();
const bool wasBlocked = blockSignals(true); const bool wasBlocked = blockSignals(true);
m_codeStyle->setCurrentDelegate(delegate); m_codeStyle->setCurrentDelegate(delegate);
@@ -227,15 +262,12 @@ void CodeStyleSelectorWidget::slotComboBoxActivated(int index)
void CodeStyleSelectorWidget::slotCurrentDelegateChanged(TextEditor::ICodeStylePreferences *delegate) void CodeStyleSelectorWidget::slotCurrentDelegateChanged(TextEditor::ICodeStylePreferences *delegate)
{ {
m_ignoreGuiSignals = true; m_ignoreGuiSignals = true;
if (m_comboBox) { m_ui->delegateComboBox->setCurrentIndex(m_ui->delegateComboBox->findData(QVariant::fromValue(delegate)));
m_comboBox->setCurrentIndex(m_comboBox->findData(QVariant::fromValue(delegate))); m_ui->delegateComboBox->setToolTip(m_ui->delegateComboBox->currentText());
m_comboBox->setToolTip(m_comboBox->currentText());
}
m_ignoreGuiSignals = false; m_ignoreGuiSignals = false;
const bool enableEdit = delegate && !delegate->isReadOnly() && !delegate->currentDelegate(); const bool removeEnabled = delegate && !delegate->isReadOnly() && !delegate->currentDelegate();
m_editButton->setEnabled(enableEdit); m_ui->removeButton->setEnabled(removeEnabled);
m_removeButton->setEnabled(enableEdit);
} }
void CodeStyleSelectorWidget::slotCopyClicked() void CodeStyleSelectorWidget::slotCopyClicked()
@@ -271,9 +303,16 @@ void CodeStyleSelectorWidget::slotEditClicked()
Internal::CodeStyleDialog dialog(m_factory, codeStyle, this); Internal::CodeStyleDialog dialog(m_factory, codeStyle, this);
if (dialog.exec() == QDialog::Accepted) { if (dialog.exec() == QDialog::Accepted) {
ICodeStylePreferences *dialogCodeStyle = dialog.codeStyle(); ICodeStylePreferences *dialogCodeStyle = dialog.codeStyle();
if (codeStyle->isReadOnly()) {
CodeStylePool *codeStylePool = m_codeStyle->delegatingPool();
codeStyle = codeStylePool->cloneCodeStyle(dialogCodeStyle);
if (codeStyle)
m_codeStyle->setCurrentDelegate(codeStyle);
return;
}
codeStyle->setTabSettings(dialogCodeStyle->tabSettings()); codeStyle->setTabSettings(dialogCodeStyle->tabSettings());
codeStyle->setValue(dialogCodeStyle->value()); codeStyle->setValue(dialogCodeStyle->value());
codeStyle->setDisplayName(dialog.displayName()); codeStyle->setDisplayName(dialogCodeStyle->displayName());
} }
} }
@@ -337,8 +376,8 @@ void CodeStyleSelectorWidget::slotCodeStyleAdded(ICodeStylePreferences *codeStyl
const QVariant data = QVariant::fromValue(codeStylePreferences); const QVariant data = QVariant::fromValue(codeStylePreferences);
const QString name = displayName(codeStylePreferences); const QString name = displayName(codeStylePreferences);
m_comboBox->addItem(name, data); m_ui->delegateComboBox->addItem(name, data);
m_comboBox->setItemData(m_comboBox->count() - 1, name, Qt::ToolTipRole); m_ui->delegateComboBox->setItemData(m_ui->delegateComboBox->count() - 1, name, Qt::ToolTipRole);
connect(codeStylePreferences, SIGNAL(displayNameChanged(QString)), connect(codeStylePreferences, SIGNAL(displayNameChanged(QString)),
this, SLOT(slotUpdateName())); this, SLOT(slotUpdateName()));
if (codeStylePreferences->delegatingPool()) { if (codeStylePreferences->delegatingPool()) {
@@ -350,7 +389,7 @@ void CodeStyleSelectorWidget::slotCodeStyleAdded(ICodeStylePreferences *codeStyl
void CodeStyleSelectorWidget::slotCodeStyleRemoved(ICodeStylePreferences *codeStylePreferences) void CodeStyleSelectorWidget::slotCodeStyleRemoved(ICodeStylePreferences *codeStylePreferences)
{ {
m_ignoreGuiSignals = true; m_ignoreGuiSignals = true;
m_comboBox->removeItem(m_comboBox->findData(QVariant::fromValue(codeStylePreferences))); m_ui->delegateComboBox->removeItem(m_ui->delegateComboBox->findData(QVariant::fromValue(codeStylePreferences)));
disconnect(codeStylePreferences, SIGNAL(displayNameChanged(QString)), disconnect(codeStylePreferences, SIGNAL(displayNameChanged(QString)),
this, SLOT(slotUpdateName())); this, SLOT(slotUpdateName()));
if (codeStylePreferences->delegatingPool()) { if (codeStylePreferences->delegatingPool()) {
@@ -375,18 +414,18 @@ void CodeStyleSelectorWidget::slotUpdateName()
updateName(codeStyle); updateName(codeStyle);
} }
m_comboBox->setToolTip(m_comboBox->currentText()); m_ui->delegateComboBox->setToolTip(m_ui->delegateComboBox->currentText());
} }
void CodeStyleSelectorWidget::updateName(ICodeStylePreferences *codeStyle) void CodeStyleSelectorWidget::updateName(ICodeStylePreferences *codeStyle)
{ {
const int idx = m_comboBox->findData(QVariant::fromValue(codeStyle)); const int idx = m_ui->delegateComboBox->findData(QVariant::fromValue(codeStyle));
if (idx < 0) if (idx < 0)
return; return;
const QString name = displayName(codeStyle); const QString name = displayName(codeStyle);
m_comboBox->setItemText(idx, name); m_ui->delegateComboBox->setItemText(idx, name);
m_comboBox->setItemData(idx, name, Qt::ToolTipRole); m_ui->delegateComboBox->setItemData(idx, name, Qt::ToolTipRole);
} }
QString CodeStyleSelectorWidget::displayName(ICodeStylePreferences *codeStyle) const QString CodeStyleSelectorWidget::displayName(ICodeStylePreferences *codeStyle) const

View File

@@ -50,11 +50,16 @@ namespace TextEditor {
class ICodeStylePreferences; class ICodeStylePreferences;
class ICodeStylePreferencesFactory; class ICodeStylePreferencesFactory;
namespace Ui {
class CodeStyleSelectorWidget;
}
class TEXTEDITOR_EXPORT CodeStyleSelectorWidget : public QWidget class TEXTEDITOR_EXPORT CodeStyleSelectorWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit CodeStyleSelectorWidget(ICodeStylePreferencesFactory *factory, QWidget *parent = 0); explicit CodeStyleSelectorWidget(ICodeStylePreferencesFactory *factory, QWidget *parent = 0);
~CodeStyleSelectorWidget();
void setCodeStyle(TextEditor::ICodeStylePreferences *codeStyle); void setCodeStyle(TextEditor::ICodeStylePreferences *codeStyle);
QString searchKeywords() const; QString searchKeywords() const;
@@ -80,15 +85,7 @@ private:
QString displayName(ICodeStylePreferences *codeStyle) const; QString displayName(ICodeStylePreferences *codeStyle) const;
QHBoxLayout *m_layout; Ui::CodeStyleSelectorWidget *m_ui;
QComboBox *m_comboBox;
QLabel *m_comboBoxLabel;
QPushButton *m_copyButton;
QPushButton *m_editButton;
QPushButton *m_removeButton;
QPushButton *m_importButton;
QPushButton *m_exportButton;
bool m_ignoreGuiSignals; bool m_ignoreGuiSignals;
}; };

View File

@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>TextEditor::CodeStyleSelectorWidget</class>
<widget class="QWidget" name="TextEditor::CodeStyleSelectorWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>523</width>
<height>58</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="margin">
<number>0</number>
</property>
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Current settings:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="delegateComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="copyButton">
<property name="text">
<string>Copy...</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="editButton">
<property name="text">
<string>Edit...</string>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QPushButton" name="removeButton">
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
<item row="0" column="4">
<widget class="QPushButton" name="exportButton">
<property name="text">
<string>Export...</string>
</property>
</widget>
</item>
<item row="1" column="4">
<widget class="QPushButton" name="importButton">
<property name="text">
<string>Import...</string>
</property>
</widget>
</item>
</layout>
</widget>
<tabstops>
<tabstop>delegateComboBox</tabstop>
<tabstop>copyButton</tabstop>
<tabstop>editButton</tabstop>
<tabstop>removeButton</tabstop>
<tabstop>exportButton</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>

View File

@@ -234,7 +234,8 @@ FORMS += \
snippets/snippetssettingspage.ui \ snippets/snippetssettingspage.ui \
behaviorsettingswidget.ui \ behaviorsettingswidget.ui \
behaviorsettingspage.ui \ behaviorsettingspage.ui \
tabsettingswidget.ui tabsettingswidget.ui \
codestyleselectorwidget.ui
RESOURCES += texteditor.qrc RESOURCES += texteditor.qrc
OTHER_FILES += TextEditor.mimetypes.xml OTHER_FILES += TextEditor.mimetypes.xml
@@ -246,3 +247,4 @@ OTHER_FILES += TextEditor.mimetypes.xml