Chris@67: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@67: Chris@67: /* Chris@67: EasyMercurial Chris@67: Chris@67: Based on HgExplorer by Jari Korhonen Chris@67: Copyright (c) 2010 Jari Korhonen Chris@67: Copyright (c) 2010 Chris Cannam Chris@67: Copyright (c) 2010 Queen Mary, University of London Chris@67: Chris@67: This program is free software; you can redistribute it and/or Chris@67: modify it under the terms of the GNU General Public License as Chris@67: published by the Free Software Foundation; either version 2 of the Chris@67: License, or (at your option) any later version. See the file Chris@67: COPYING included with this distribution for more information. Chris@67: */ Chris@67: Chris@67: #include "multichoicedialog.h" Chris@67: Chris@69: #include "selectablelabel.h" Chris@69: Chris@69: #include "debug.h" Chris@69: Chris@68: #include Chris@69: #include Chris@69: #include Chris@69: #include Chris@69: #include Chris@69: #include Chris@68: Chris@68: MultiChoiceDialog::MultiChoiceDialog(QString title, QString heading, QWidget *parent) : Chris@67: QDialog(parent) Chris@67: { Chris@68: setModal(true); Chris@68: setWindowTitle(title); Chris@68: Chris@68: QGridLayout *outer = new QGridLayout; Chris@68: setLayout(outer); Chris@68: Chris@68: outer->addWidget(new QLabel(heading), 0, 0, 1, 3); Chris@68: Chris@68: QWidget *innerWidget = new QWidget; Chris@69: outer->addWidget(innerWidget, 1, 0, 1, 3); Chris@69: m_choiceLayout = new QHBoxLayout; Chris@68: innerWidget->setLayout(m_choiceLayout); Chris@68: Chris@68: m_descriptionLabel = new QLabel; Chris@68: outer->addWidget(m_descriptionLabel, 2, 0, 1, 3); Chris@68: Chris@69: QFont f = m_descriptionLabel->font(); Chris@69: f.setPointSize(f.pointSize() * 0.9); Chris@69: m_descriptionLabel->setFont(f); Chris@69: Chris@68: m_argLabel = new QLabel(); Chris@68: outer->addWidget(m_argLabel, 3, 0); Chris@68: Chris@69: m_argEdit = new QComboBox(); Chris@69: m_argEdit->setEditable(true); Chris@68: outer->addWidget(m_argEdit, 3, 1); Chris@69: outer->setColumnStretch(1, 20); Chris@68: Chris@68: m_browseButton = new QPushButton(tr("Browse...")); Chris@68: outer->addWidget(m_browseButton, 3, 2); Chris@69: connect(m_browseButton, SIGNAL(clicked()), this, SLOT(browse())); Chris@68: Chris@68: QDialogButtonBox *bbox = new QDialogButtonBox(QDialogButtonBox::Ok | Chris@68: QDialogButtonBox::Cancel); Chris@68: connect(bbox, SIGNAL(accepted()), this, SLOT(accept())); Chris@68: connect(bbox, SIGNAL(rejected()), this, SLOT(reject())); Chris@68: outer->addWidget(bbox, 4, 0, 1, 3); Chris@69: Chris@69: setMinimumWidth(480); Chris@67: } Chris@68: Chris@69: QString Chris@69: MultiChoiceDialog::getCurrentChoice() Chris@69: { Chris@69: return m_currentChoice; Chris@69: } Chris@69: Chris@69: void Chris@69: MultiChoiceDialog::setCurrentChoice(QString c) Chris@69: { Chris@69: m_currentChoice = c; Chris@69: choiceChanged(); Chris@69: } Chris@69: Chris@69: QString Chris@69: MultiChoiceDialog::getArgument() Chris@69: { Chris@69: return m_argEdit->currentText(); Chris@69: } Chris@69: Chris@69: void Chris@69: MultiChoiceDialog::addRecentArgument(QString id, QString arg) Chris@69: { Chris@69: RecentFiles(QString("Recent-%1").arg(id)).addFile(arg); Chris@69: } Chris@69: Chris@69: void Chris@69: MultiChoiceDialog::addChoice(QString id, QString text, Chris@69: QString description, ArgType arg) Chris@69: { Chris@69: bool first = (m_texts.empty()); Chris@69: Chris@69: m_texts[id] = text; Chris@69: m_descriptions[id] = description; Chris@69: m_argTypes[id] = arg; Chris@69: Chris@69: if (arg != NoArg) { Chris@69: m_recentFiles[id] = QSharedPointer Chris@69: (new RecentFiles(QString("Recent-%1").arg(id))); Chris@69: } Chris@69: Chris@69: SelectableLabel *cb = new SelectableLabel; Chris@69: cb->setSelectedText(text); Chris@69: cb->setUnselectedText(text); Chris@69: Chris@69: m_choiceLayout->addWidget(cb); Chris@69: m_choiceButtons[cb] = id; Chris@69: Chris@69: connect(cb, SIGNAL(selectionChanged()), this, SLOT(choiceChanged())); Chris@69: Chris@69: if (first) { Chris@69: m_currentChoice = id; Chris@69: choiceChanged(); Chris@69: } Chris@69: } Chris@69: Chris@69: void Chris@69: MultiChoiceDialog::browse() Chris@69: { Chris@69: QString origin = getArgument(); Chris@69: Chris@69: if (origin == "") { Chris@69: #ifdef Q_OS_WIN32 Chris@69: origin = "c:"; Chris@69: #else Chris@69: origin = QDir::homePath(); Chris@69: #endif Chris@69: } Chris@69: Chris@69: QString path = origin; Chris@69: Chris@69: if (m_argTypes[m_currentChoice] == DirectoryArg) { Chris@69: Chris@69: path = QFileDialog::getExistingDirectory Chris@69: (this, tr("Open Directory"), origin, Chris@69: QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); Chris@69: if (path != QString()) { Chris@69: m_argEdit->lineEdit()->setText(path + QDir::separator()); Chris@69: } Chris@69: Chris@69: } else { Chris@69: Chris@69: path = QFileDialog::getOpenFileName Chris@69: (this, tr("Open File"), origin); Chris@69: if (path != QString()) { Chris@69: m_argEdit->lineEdit()->setText(path); Chris@69: } Chris@69: } Chris@69: } Chris@69: Chris@69: void Chris@69: MultiChoiceDialog::choiceChanged() Chris@69: { Chris@69: DEBUG << "choiceChanged" << endl; Chris@69: Chris@69: if (m_choiceButtons.empty()) return; Chris@69: Chris@69: QString id = ""; Chris@69: Chris@69: QObject *s = sender(); Chris@69: QWidget *w = qobject_cast(s); Chris@69: if (w) id = m_choiceButtons[w]; Chris@69: Chris@69: if (id == m_currentChoice) return; Chris@69: if (id == "") { Chris@69: // Happens when this is called for the very first time, when Chris@69: // m_currentChoice has been set to the intended ID but no Chris@69: // button has actually been pressed -- then we need to Chris@69: // initialise Chris@69: id = m_currentChoice; Chris@69: } Chris@69: Chris@69: m_currentChoice = id; Chris@69: Chris@69: foreach (QWidget *cw, m_choiceButtons.keys()) { Chris@69: SelectableLabel *sl = qobject_cast(cw); Chris@69: if (sl) { Chris@69: sl->setSelected(m_choiceButtons[cw] == id); Chris@69: } Chris@69: } Chris@69: Chris@69: m_descriptionLabel->setText(m_descriptions[id]); Chris@69: Chris@69: switch (m_argTypes[id]) { Chris@69: Chris@69: case NoArg: Chris@69: m_argLabel->hide(); Chris@69: m_argEdit->hide(); Chris@69: m_browseButton->hide(); Chris@69: break; Chris@69: Chris@69: case FileArg: Chris@69: m_argLabel->setText(tr("File:")); Chris@69: m_argLabel->show(); Chris@69: m_argEdit->show(); Chris@69: m_browseButton->show(); Chris@69: break; Chris@69: Chris@69: case DirectoryArg: Chris@69: m_argLabel->setText(tr("Folder:")); Chris@69: m_argLabel->show(); Chris@69: m_argEdit->show(); Chris@69: m_browseButton->show(); Chris@69: break; Chris@69: Chris@69: case UrlArg: Chris@69: m_argLabel->setText(tr("URL:")); Chris@69: m_argLabel->show(); Chris@69: m_argEdit->show(); Chris@69: m_browseButton->hide(); Chris@69: break; Chris@69: Chris@69: case FileOrUrlArg: Chris@69: m_argLabel->setText(tr("File or URL:")); Chris@69: m_argLabel->show(); Chris@69: m_argEdit->show(); Chris@69: m_browseButton->show(); Chris@69: break; Chris@69: } Chris@69: Chris@69: if (m_argTypes[id] != NoArg) { Chris@69: QSharedPointer rf = m_recentFiles[id]; Chris@69: m_argEdit->clear(); Chris@69: m_argEdit->addItems(rf->getRecent()); Chris@69: } Chris@69: Chris@69: adjustSize(); Chris@69: } Chris@69: Chris@69: