1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2010 Nicolas Arnaud-Cormos.
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "macrooptionswidget.h"
35 #include "ui_macrooptionswidget.h"
36 #include "macrosconstants.h"
37 #include "macromanager.h"
39 #include "macrosconstants.h"
41 #include <coreplugin/icore.h>
42 #include <coreplugin/coreconstants.h>
43 #include <coreplugin/actionmanager/actionmanager.h>
44 #include <coreplugin/actionmanager/command.h>
45 #include <coreplugin/uniqueidmanager.h>
47 #include <QtCore/QDir>
48 #include <QtCore/QFileInfo>
50 #include <QtGui/QShortcut>
51 #include <QtGui/QButtonGroup>
52 #include <QtGui/QTreeWidget>
53 #include <QtGui/QTreeWidgetItem>
54 #include <QtGui/QCheckBox>
55 #include <QtGui/QGroupBox>
56 #include <QtGui/QHeaderView>
57 #include <QtGui/QRegExpValidator>
58 #include <QtGui/QLineEdit>
61 int NAME_ROLE = Qt::UserRole;
62 int WRITE_ROLE = Qt::UserRole+1;
65 using namespace Macros;
66 using namespace Macros::Internal;
69 MacroOptionsWidget::MacroOptionsWidget(QWidget *parent) :
71 m_ui(new Ui::MacroOptionsWidget),
72 m_changingCurrent(false)
76 connect(m_ui->treeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
77 this, SLOT(changeCurrentItem(QTreeWidgetItem*)));
78 connect(m_ui->removeButton, SIGNAL(clicked()),
79 this, SLOT(remove()));
80 connect(m_ui->description, SIGNAL(textChanged(QString)),
81 this, SLOT(changeDescription(QString)));
83 m_ui->treeWidget->header()->setSortIndicator(0, Qt::AscendingOrder);
88 MacroOptionsWidget::~MacroOptionsWidget()
93 void MacroOptionsWidget::initialize()
95 m_macroToRemove.clear();
96 m_macroToChange.clear();
97 m_ui->treeWidget->clear();
99 // Create the treeview
103 void MacroOptionsWidget::createTable()
105 QDir dir(MacroManager::instance()->macrosDirectory());
106 Core::ICore *core = Core::ICore::instance();
107 Core::ActionManager *am = core->actionManager();
109 QMapIterator<QString, Macro *> it(MacroManager::instance()->macros());
110 while (it.hasNext()) {
112 QFileInfo fileInfo(it.value()->fileName());
113 if (fileInfo.absoluteDir() == dir.absolutePath()) {
114 QTreeWidgetItem *macroItem = new QTreeWidgetItem(m_ui->treeWidget);
115 macroItem->setText(0, it.value()->displayName());
116 macroItem->setText(1, it.value()->description());
117 macroItem->setData(0, NAME_ROLE, it.value()->displayName());
118 macroItem->setData(0, WRITE_ROLE, it.value()->isWritable());
120 Core::Command *command = am->command(Core::Id(Constants::PREFIX_MACRO+it.value()->displayName()));
121 if (command && command->shortcut())
122 macroItem->setText(2, command->shortcut()->key().toString());
127 void MacroOptionsWidget::changeCurrentItem(QTreeWidgetItem *current)
129 m_changingCurrent = true;
131 m_ui->removeButton->setEnabled(false);
132 m_ui->description->clear();
133 m_ui->macroGroup->setEnabled(false);
135 m_ui->removeButton->setEnabled(true);
136 m_ui->description->setText(current->text(1));
137 m_ui->description->setEnabled(current->data(0, WRITE_ROLE).toBool());
138 m_ui->macroGroup->setEnabled(true);
140 m_changingCurrent = false;
143 void MacroOptionsWidget::remove()
145 QTreeWidgetItem *current = m_ui->treeWidget->currentItem();
146 m_macroToRemove.append(current->data(0, NAME_ROLE).toString());
150 void MacroOptionsWidget::apply()
153 foreach (const QString &name, m_macroToRemove) {
154 MacroManager::instance()->deleteMacro(name);
155 m_macroToChange.remove(name);
159 QMapIterator<QString, QString> it(m_macroToChange);
160 while (it.hasNext()) {
162 MacroManager::instance()->changeMacro(it.key(), it.value());
165 // Reinitialize the page
169 void MacroOptionsWidget::changeDescription(const QString &description)
171 QTreeWidgetItem *current = m_ui->treeWidget->currentItem();
172 if (m_changingCurrent || !current)
175 QString macroName = current->data(0, NAME_ROLE).toString();
176 m_macroToChange[macroName] = description;
177 current->setText(1, description);
178 QFont font = current->font(1);
179 font.setItalic(true);
180 current->setFont(1, font);