1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
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 "gitorious.h"
35 #include "gitorioushostwidget.h"
36 #include "ui_gitorioushostwidget.h"
38 #include <coreplugin/coreconstants.h>
40 #include <QtCore/QUrl>
41 #include <QtCore/QDebug>
42 #include <QtCore/QTimer>
44 #include <QtGui/QStandardItem>
45 #include <QtGui/QStandardItemModel>
46 #include <QtGui/QItemSelectionModel>
47 #include <QtGui/QDesktopServices>
48 #include <QtGui/QIcon>
49 #include <QtGui/QStyle>
56 enum { HostNameColumn, ProjectCountColumn, DescriptionColumn, ColumnCount };
58 // Create a model row for a host. Make the host name editable as specified by
60 static QList<QStandardItem *> hostEntry(const QString &host,
62 const QString &description, bool isDummyEntry)
64 const Qt::ItemFlags nonEditableFlags = (Qt::ItemIsSelectable|Qt::ItemIsEnabled);
65 const Qt::ItemFlags editableFlags = nonEditableFlags|Qt::ItemIsEditable;
66 QStandardItem *hostItem = new QStandardItem(host);
67 hostItem->setFlags(isDummyEntry ? editableFlags : nonEditableFlags);
68 // Empty for dummy, else "..." or count
69 QStandardItem *projectCountItem = 0;
70 QString countItemText;
72 countItemText = projectCount ? QString::number(projectCount) : QString(QLatin1String("..."));
74 projectCountItem = new QStandardItem(countItemText);
75 projectCountItem->setFlags(nonEditableFlags);
76 QStandardItem *descriptionItem = new QStandardItem(description);
77 descriptionItem->setFlags(editableFlags);
78 QList<QStandardItem *> rc;
79 rc << hostItem << projectCountItem << descriptionItem;
83 static inline QList<QStandardItem *> hostEntry(const GitoriousHost &h)
85 return hostEntry(h.hostName, h.projects.size(), h.description, false);
88 GitoriousHostWidget::GitoriousHostWidget(QWidget *parent) :
90 m_newHost(tr("<New Host>")),
91 ui(new Ui::GitoriousHostWidget),
92 m_model(new QStandardItemModel(0, ColumnCount)),
95 m_isHostListDirty(false)
98 ui->errorLabel->setVisible(false);
99 ui->browseToolButton->setIcon(style()->standardIcon(QStyle::SP_MessageBoxInformation));
100 connect(ui->browseToolButton, SIGNAL(clicked()), this, SLOT(slotBrowse()));
101 ui->browseToolButton->setEnabled(false);
102 ui->deleteToolButton->setIcon(QIcon(Core::Constants::ICON_MINUS));
103 connect(ui->deleteToolButton, SIGNAL(clicked()), this, SLOT(slotDelete()));
104 ui->deleteToolButton->setEnabled(false);
108 headers << tr("Host") << tr("Projects") << tr("Description");
109 m_model->setHorizontalHeaderLabels(headers);
111 Gitorious &gitorious = Gitorious::instance();
112 foreach( const GitoriousHost &gh, gitorious.hosts())
113 m_model->appendRow(hostEntry(gh));
114 appendNewDummyEntry();
115 connect(m_model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotItemEdited(QStandardItem*)));
116 ui->hostView->setModel(m_model);
119 ui->hostView->setRootIsDecorated(false);
120 ui->hostView->setUniformRowHeights(true);
121 connect(ui->hostView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
122 this, SLOT(slotCurrentChanged(QModelIndex,QModelIndex)));
124 ui->hostView->setSelectionMode(QAbstractItemView::SingleSelection);
125 if (m_model->rowCount())
128 connect(&gitorious, SIGNAL(projectListPageReceived(int,int)),
129 this, SLOT(slotProjectListPageReceived(int)));
130 connect(&gitorious, SIGNAL(projectListReceived(int)),
131 this, SLOT(slotProjectListPageReceived(int)));
133 connect(&gitorious, SIGNAL(error(QString)), this, SLOT(slotError(QString)));
135 setMinimumWidth(700);
138 GitoriousHostWidget::~GitoriousHostWidget()
141 Gitorious *gitorious = &Gitorious::instance();
142 disconnect(gitorious, SIGNAL(projectListPageReceived(int,int)),
143 this, SLOT(slotProjectListPageReceived(int)));
144 disconnect(gitorious, SIGNAL(projectListReceived(int)),
145 this, SLOT(slotProjectListPageReceived(int)));
146 disconnect(gitorious, SIGNAL(error(QString)), this, SLOT(slotError(QString)));
150 int GitoriousHostWidget::selectedRow() const
152 const QModelIndex idx = ui->hostView->selectionModel()->currentIndex();
158 void GitoriousHostWidget::selectRow(int r)
160 if (r >= 0 && r != selectedRow()) {
161 const QModelIndex index = m_model->index(r, 0);
162 ui->hostView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select|QItemSelectionModel::Current|QItemSelectionModel::Rows);
166 void GitoriousHostWidget::appendNewDummyEntry()
168 // Append a new entry where a host name is editable
169 m_model->appendRow(hostEntry(m_newHost, 0, QString(), true));
172 void GitoriousHostWidget::slotItemEdited(QStandardItem *item)
174 // Synchronize with Gitorious singleton.
175 // Did someone enter a valid host name into the dummy item?
176 // -> Create a new one.
177 const int row = item->row();
178 const bool isDummyEntry = row >= Gitorious::instance().hostCount();
179 switch (item->column()) {
182 Gitorious::instance().addHost(item->text(), m_model->item(row, DescriptionColumn)->text());
183 m_isHostListDirty = true;
184 appendNewDummyEntry();
188 case ProjectCountColumn:
190 case DescriptionColumn:
192 const QString description = item->text();
193 if (description != Gitorious::instance().hostDescription(row)) {
194 Gitorious::instance().setHostDescription(row, item->text());
195 m_isHostListDirty = true;
202 void GitoriousHostWidget::slotProjectListPageReceived(int row)
205 qDebug() << Q_FUNC_INFO << row;
207 const int projectCount = Gitorious::instance().projectCount(row);
208 m_model->item(row, ProjectCountColumn)->setText(QString::number(projectCount));
209 // If it is the currently selected host, re-check validity if not enabled
211 const QModelIndex current = ui->hostView->selectionModel()->currentIndex();
212 if (current.isValid() && current.row() == row)
217 QStandardItem *GitoriousHostWidget::currentItem() const
219 const QModelIndex idx = ui->hostView->selectionModel()->currentIndex();
221 return m_model->itemFromIndex(idx.column() != 0 ? idx.sibling(idx.row(), 0) : idx);
225 void GitoriousHostWidget::slotBrowse()
227 if (const QStandardItem *item = currentItem()) {
228 const QUrl url(QLatin1String("http://") + item->text() + QLatin1Char('/'));
230 QDesktopServices::openUrl(url);
234 void GitoriousHostWidget::slotDelete()
236 const QModelIndex index = ui->hostView->selectionModel()->currentIndex();
237 ui->hostView->selectionModel()->clear();
238 if (index.isValid()) {
239 const int row = index.row();
240 qDeleteAll(m_model->takeRow(row));
241 Gitorious::instance().removeAt(row);
242 m_isHostListDirty = true;
246 void GitoriousHostWidget::slotCurrentChanged(const QModelIndex ¤t, const QModelIndex & /* previous */)
251 void GitoriousHostWidget::checkValid(const QModelIndex &index)
254 qDebug() << Q_FUNC_INFO << index;
255 bool hasSelectedHost = false;
256 bool hasProjects = false;
257 if (index.isValid()) {
258 // Are we on the new dummy item?
259 Gitorious &gitorious = Gitorious::instance();
260 const int row = index.row();
261 hasSelectedHost = row < gitorious.hostCount();
262 hasProjects = hasSelectedHost && gitorious.projectCount(row) > 0;
264 ui->deleteToolButton->setEnabled(hasSelectedHost);
265 ui->browseToolButton->setEnabled(hasSelectedHost);
267 const bool valid = hasSelectedHost && hasProjects;
268 if (valid != m_isValid) {
274 bool GitoriousHostWidget::isValid() const
279 bool GitoriousHostWidget::isHostListDirty() const
281 return m_isHostListDirty;
284 void GitoriousHostWidget::slotClearError()
286 ui->errorLabel->setVisible(false);
287 ui->errorLabel->clear();
290 void GitoriousHostWidget::slotError(const QString &e)
292 // Display error for a while
293 ui->errorLabel->setText(e);
294 ui->errorLabel->setVisible(true);
295 if (!m_errorClearTimer) {
296 m_errorClearTimer = new QTimer(this);
297 m_errorClearTimer->setSingleShot(true);
298 m_errorClearTimer->setInterval(5000);
299 connect(m_errorClearTimer, SIGNAL(timeout()), this, SLOT(slotClearError()));
301 if (!m_errorClearTimer->isActive())
302 m_errorClearTimer->start();
305 void GitoriousHostWidget::changeEvent(QEvent *e)
307 QWidget::changeEvent(e);
309 case QEvent::LanguageChange:
310 ui->retranslateUi(this);
317 } // namespace Internal
318 } // namespace Gitorious