OSDN Git Service

Still changes to our options dialog
[qt-creator-jp/qt-creator-jp.git] / src / plugins / macros / macrooptionswidget.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2010 Nicolas Arnaud-Cormos.
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "macrooptionswidget.h"
35 #include "ui_macrooptionswidget.h"
36 #include "macrosconstants.h"
37 #include "macromanager.h"
38 #include "macro.h"
39 #include "macrosconstants.h"
40
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>
46
47 #include <QtCore/QDir>
48 #include <QtCore/QFileInfo>
49
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>
59
60 namespace {
61     int NAME_ROLE = Qt::UserRole;
62     int WRITE_ROLE = Qt::UserRole+1;
63 }
64
65 using namespace Macros;
66 using namespace Macros::Internal;
67
68
69 MacroOptionsWidget::MacroOptionsWidget(QWidget *parent) :
70     QWidget(parent),
71     m_ui(new Ui::MacroOptionsWidget),
72     m_changingCurrent(false)
73 {
74     m_ui->setupUi(this);
75
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)));
82
83     m_ui->treeWidget->header()->setSortIndicator(0, Qt::AscendingOrder);
84
85     initialize();
86 }
87
88 MacroOptionsWidget::~MacroOptionsWidget()
89 {
90     delete m_ui;
91 }
92
93 void MacroOptionsWidget::initialize()
94 {
95     m_macroToRemove.clear();
96     m_macroToChange.clear();
97     m_ui->treeWidget->clear();
98
99     // Create the treeview
100     createTable();
101 }
102
103 void MacroOptionsWidget::createTable()
104 {
105     QDir dir(MacroManager::instance()->macrosDirectory());
106     Core::ICore *core = Core::ICore::instance();
107     Core::ActionManager *am = core->actionManager();
108
109     QMapIterator<QString, Macro *> it(MacroManager::instance()->macros());
110     while (it.hasNext()) {
111         it.next();
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());
119
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());
123         }
124     }
125 }
126
127 void MacroOptionsWidget::changeCurrentItem(QTreeWidgetItem *current)
128 {
129     m_changingCurrent = true;
130     if (!current) {
131         m_ui->removeButton->setEnabled(false);
132         m_ui->description->clear();
133         m_ui->macroGroup->setEnabled(false);
134     } else {
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);
139     }
140     m_changingCurrent = false;
141 }
142
143 void MacroOptionsWidget::remove()
144 {
145     QTreeWidgetItem *current = m_ui->treeWidget->currentItem();
146     m_macroToRemove.append(current->data(0, NAME_ROLE).toString());
147     delete current;
148 }
149
150 void MacroOptionsWidget::apply()
151 {
152     // Remove macro
153     foreach (const QString &name, m_macroToRemove) {
154         MacroManager::instance()->deleteMacro(name);
155         m_macroToChange.remove(name);
156     }
157
158     // Change macro
159     QMapIterator<QString, QString> it(m_macroToChange);
160     while (it.hasNext()) {
161         it.next();
162         MacroManager::instance()->changeMacro(it.key(), it.value());
163     }
164
165     // Reinitialize the page
166     initialize();
167 }
168
169 void MacroOptionsWidget::changeDescription(const QString &description)
170 {
171     QTreeWidgetItem *current = m_ui->treeWidget->currentItem();
172     if (m_changingCurrent || !current)
173         return;
174
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);
181 }