OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / generichighlighter / managedefinitionsdialog.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 (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 "managedefinitionsdialog.h"
35 #include "manager.h"
36
37 #include <QtCore/QUrl>
38 #include <QtCore/QIODevice>
39 #include <QtCore/QXmlStreamReader>
40 #include <QtCore/QXmlStreamAttributes>
41 #include <QtCore/QFuture>
42 #include <QtCore/QFutureWatcher>
43 #include <QtCore/QtConcurrentMap>
44 #include <QtCore/QFile>
45 #include <QtCore/QFileInfo>
46 #include <QtGui/QMessageBox>
47
48 #include <QtCore/QDebug>
49
50 using namespace TextEditor;
51 using namespace Internal;
52
53 ManageDefinitionsDialog::ManageDefinitionsDialog(
54         const QList<HighlightDefinitionMetaData> &metaDataList,
55         const QString &path,
56         QWidget *parent) :
57     QDialog(parent),
58     m_definitionsMetaData(metaDataList),
59     m_path(path)
60 {
61     ui.setupUi(this);    
62     ui.definitionsTable->setHorizontalHeaderLabels(
63         QStringList() << tr("Name") << tr("Installed") << tr("Available"));
64     ui.definitionsTable->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
65
66     setWindowTitle(tr("Download Definitions"));
67
68     populateDefinitionsWidget();
69
70     connect(ui.downloadButton, SIGNAL(clicked()), this, SLOT(downloadDefinitions()));
71     connect(ui.allButton, SIGNAL(clicked()), this, SLOT(selectAll()));
72     connect(ui.clearButton, SIGNAL(clicked()), this, SLOT(clearSelection()));
73     connect(ui.invertButton, SIGNAL(clicked()), this, SLOT(invertSelection()));
74 }
75
76 void ManageDefinitionsDialog::populateDefinitionsWidget()
77 {
78     const int size = m_definitionsMetaData.size();
79     ui.definitionsTable->setRowCount(size);
80     for (int i = 0; i < size; ++i) {
81         const HighlightDefinitionMetaData &downloadData = m_definitionsMetaData.at(i);
82
83         // Look for this definition in the current path specified by the user, not the one
84         // stored in the settings. So the manager should not be queried for this information.
85         QString dirVersion;
86         QFileInfo fi(m_path + downloadData.fileName());
87         QFile definitionFile(fi.absoluteFilePath());
88         if (definitionFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
89             const QSharedPointer<HighlightDefinitionMetaData> &data = Manager::parseMetadata(fi);
90             if (!data.isNull())
91                 dirVersion = data->version();
92         }
93
94         for (int j = 0; j < 3; ++j) {
95             QTableWidgetItem *item = new QTableWidgetItem;
96             if (j == 0)
97                 item->setText(downloadData.name());
98             else if (j == 1) {
99                 item->setText(dirVersion);
100                 item->setTextAlignment(Qt::AlignCenter);
101             } else if (j == 2) {
102                 item->setText(downloadData.version());
103                 item->setTextAlignment(Qt::AlignCenter);
104             }
105             ui.definitionsTable->setItem(i, j, item);
106         }
107     }
108 }
109
110 void ManageDefinitionsDialog::downloadDefinitions()
111 {
112     if (Manager::instance()->isDownloadingDefinitions()) {
113         QMessageBox::information(
114             this,
115             tr("Download Information"),
116             tr("There is already one download in progress. Please wait until it is finished."));
117         return;
118     }
119
120     QList<QUrl> urls;
121     foreach (const QModelIndex &index, ui.definitionsTable->selectionModel()->selectedRows())
122         urls.append(m_definitionsMetaData.at(index.row()).url());
123     Manager::instance()->downloadDefinitions(urls, m_path);
124     accept();
125 }
126
127 void ManageDefinitionsDialog::selectAll()
128 {
129     ui.definitionsTable->selectAll();
130     ui.definitionsTable->setFocus();
131 }
132
133 void ManageDefinitionsDialog::clearSelection()
134 {
135     ui.definitionsTable->clearSelection();
136 }
137
138 void ManageDefinitionsDialog::invertSelection()
139 {
140     const QModelIndex &topLeft = ui.definitionsTable->model()->index(0, 0);
141     const QModelIndex &bottomRight =
142         ui.definitionsTable->model()->index(ui.definitionsTable->rowCount() - 1,
143                                             ui.definitionsTable->columnCount() - 1);
144     QItemSelection itemSelection(topLeft, bottomRight);
145     ui.definitionsTable->selectionModel()->select(itemSelection, QItemSelectionModel::Toggle);
146     ui.definitionsTable->setFocus();
147 }
148
149 void ManageDefinitionsDialog::changeEvent(QEvent *e)
150 {
151     QDialog::changeEvent(e);
152     switch (e->type()) {
153     case QEvent::LanguageChange:
154         ui.retranslateUi(this);
155         break;
156     default:
157         break;
158     }
159 }