OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / coreplugin / actionmanager / commandmappings.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "commandmappings.h"
34 #include "ui_commandmappings.h"
35 #include "actionmanager_p.h"
36 #include "actionmanager/command.h"
37 #include "command_p.h"
38 #include "commandsfile.h"
39 #include "coreconstants.h"
40 #include "filemanager.h"
41 #include "icore.h"
42 #include "uniqueidmanager.h"
43
44 #include <utils/treewidgetcolumnstretcher.h>
45
46 #include <QtGui/QKeyEvent>
47 #include <QtGui/QShortcut>
48 #include <QtGui/QHeaderView>
49 #include <QtGui/QTreeWidgetItem>
50 #include <QtGui/QFileDialog>
51 #include <QtCore/QCoreApplication>
52 #include <QtDebug>
53
54 Q_DECLARE_METATYPE(Core::Internal::ShortcutItem*)
55
56 using namespace Core;
57 using namespace Core::Internal;
58
59 CommandMappings::CommandMappings(QObject *parent)
60     : IOptionsPage(parent), m_page(0)
61 {
62 }
63
64 CommandMappings::~CommandMappings()
65 {
66 }
67
68 // IOptionsPage
69
70 QWidget *CommandMappings::createPage(QWidget *parent)
71 {
72     m_page = new Ui_CommandMappings();
73     QWidget *w = new QWidget(parent);
74     m_page->setupUi(w);
75     m_page->targetEdit->setAutoHideButton(Utils::FancyLineEdit::Right, true);
76     m_page->targetEdit->installEventFilter(this);
77
78     connect(m_page->targetEdit, SIGNAL(buttonClicked(Utils::FancyLineEdit::Side)),
79         this, SLOT(removeTargetIdentifier()));
80     connect(m_page->resetButton, SIGNAL(clicked()),
81         this, SLOT(resetTargetIdentifier()));
82     connect(m_page->exportButton, SIGNAL(clicked()),
83         this, SLOT(exportAction()));
84     connect(m_page->importButton, SIGNAL(clicked()),
85         this, SLOT(importAction()));
86     connect(m_page->defaultButton, SIGNAL(clicked()),
87         this, SLOT(defaultAction()));
88
89     initialize();
90
91     m_page->commandList->sortByColumn(0, Qt::AscendingOrder);
92
93     connect(m_page->filterEdit, SIGNAL(textChanged(QString)),
94         this, SLOT(filterChanged(QString)));
95     connect(m_page->commandList, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
96         this, SLOT(commandChanged(QTreeWidgetItem *)));
97     connect(m_page->targetEdit, SIGNAL(textChanged(QString)),
98         this, SLOT(targetIdentifierChanged()));
99
100     new Utils::TreeWidgetColumnStretcher(m_page->commandList, 1);
101
102     commandChanged(0);
103
104     return w;
105 }
106
107 void CommandMappings::setImportExportEnabled(bool enabled)
108 {
109     m_page->importButton->setVisible(enabled);
110     m_page->exportButton->setVisible(enabled);
111 }
112
113 QTreeWidget *CommandMappings::commandList() const
114 {
115     return m_page->commandList;
116 }
117
118 QLineEdit *CommandMappings::targetEdit() const
119 {
120     return m_page->targetEdit;
121 }
122
123 void CommandMappings::setPageTitle(const QString &s)
124 {
125     m_page->groupBox->setTitle(s);
126 }
127
128 void CommandMappings::setTargetLabelText(const QString &s)
129 {
130     m_page->targetEditLabel->setText(s);
131 }
132
133 void CommandMappings::setTargetEditTitle(const QString &s)
134 {
135     m_page->targetEditGroup->setTitle(s);
136 }
137
138 void CommandMappings::setTargetHeader(const QString &s)
139 {
140     m_page->commandList->setHeaderLabels(QStringList() << tr("Command") << tr("Label") << s);
141 }
142
143 void CommandMappings::finish()
144 {
145     if (!m_page) // page was never shown
146         return;
147     delete m_page;
148     m_page = 0;
149 }
150
151 void CommandMappings::commandChanged(QTreeWidgetItem *current)
152 {
153     if (!current || !current->data(0, Qt::UserRole).isValid()) {
154         m_page->targetEdit->setText("");
155         m_page->targetEditGroup->setEnabled(false);
156         return;
157     }
158     m_page->targetEditGroup->setEnabled(true);
159 }
160
161 void CommandMappings::filterChanged(const QString &f)
162 {
163     if (!m_page)
164         return;
165     for (int i=0; i<m_page->commandList->topLevelItemCount(); ++i) {
166         QTreeWidgetItem *item = m_page->commandList->topLevelItem(i);
167         item->setHidden(filter(f, item));
168     }
169 }
170
171 bool CommandMappings::filter(const QString &f, const QTreeWidgetItem *item)
172 {
173     if (QTreeWidgetItem *parent = item->parent()) {
174         if (parent->text(0).contains(f, Qt::CaseInsensitive))
175             return false;
176     }
177
178     if (item->childCount() == 0) {
179         if (f.isEmpty())
180             return false;
181         for (int i = 0; i < item->columnCount(); ++i) {
182             if (item->text(i).contains(f, Qt::CaseInsensitive))
183                 return false;
184         }
185         return true;
186     }
187
188     bool found = false;
189     for (int i = 0; i < item->childCount(); ++i) {
190         QTreeWidgetItem *citem = item->child(i);
191         if (filter(f, citem)) {
192             citem->setHidden(true);
193         } else {
194             citem->setHidden(false);
195             found = true;
196         }
197     }
198     return !found;
199 }
200
201 void CommandMappings::setModified(QTreeWidgetItem *item , bool modified)
202 {
203     QFont f = item->font(0);
204     f.setItalic(modified);
205     item->setFont(0, f);
206     item->setFont(1, f);
207     f.setBold(modified);
208     item->setFont(2, f);
209 }
210
211 QString CommandMappings::filterText() const
212 {
213     if (!m_page)
214         return QString();
215     return m_page->filterEdit->text();
216 }