File indexing completed on 2024-04-28 04:34:27

0001 /*
0002  * This file is part of KDevelop Krazy2 Plugin.
0003  *
0004  * Copyright 2012 Daniel Calviño Sánchez <danxuliu@gmail.com>
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License
0008  * as published by the Free Software Foundation; either version 2
0009  * of the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program. If not, see <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "selectcheckerswidget.h"
0021 
0022 #include "../checker.h"
0023 #include "../checkermodel.h"
0024 
0025 #include "ui_selectcheckerswidget.h"
0026 
0027 //public:
0028 
0029 SelectCheckersWidget::SelectCheckersWidget(QWidget* parent /*= 0*/): QWidget(parent) {
0030     m_ui = new Ui::SelectCheckersWidget();
0031     m_ui->setupUi(this);
0032 
0033     m_otherAvailableCheckersModel = new CheckerModel(this);
0034     m_ui->otherAvailableCheckersView->setModel(m_otherAvailableCheckersModel);
0035     m_ui->otherAvailableCheckersView->header()->hide();
0036     m_ui->otherAvailableCheckersView->setSelectionMode(QAbstractItemView::ExtendedSelection);
0037 
0038     connect(m_ui->otherAvailableCheckersView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
0039             this, SLOT(handleOtherAvailableCheckersSelectionChanged()));
0040 
0041     m_checkersToRunModel = new CheckerModel(this);
0042     m_ui->checkersToRunView->setModel(m_checkersToRunModel);
0043     m_ui->checkersToRunView->header()->hide();
0044     m_ui->checkersToRunView->setSelectionMode(QAbstractItemView::ExtendedSelection);
0045 
0046     connect(m_ui->checkersToRunView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
0047             this, SLOT(handleCheckersToRunSelectionChanged()));
0048 
0049     connect(m_ui->addButton, SIGNAL(clicked()),
0050             this, SLOT(add()));
0051     connect(m_ui->removeButton, SIGNAL(clicked()),
0052             this, SLOT(remove()));
0053 }
0054 
0055 SelectCheckersWidget::~SelectCheckersWidget() {
0056     delete m_ui;
0057 }
0058 
0059 QList<const Checker*> SelectCheckersWidget::checkersToRun() const {
0060     return m_checkersToRun;
0061 }
0062 
0063 void SelectCheckersWidget::setCheckers(const QList<const Checker*>& availableCheckers,
0064                                        const QList<const Checker*>& checkersToRun) {
0065     m_otherAvailableCheckers.clear();
0066     foreach (const Checker* checker, availableCheckers) {
0067         if (!checkersToRun.contains(checker)) {
0068             m_otherAvailableCheckers.append(checker);
0069         }
0070     }
0071 
0072     m_checkersToRun = checkersToRun;
0073 
0074     updateCheckers();
0075 
0076     m_ui->otherAvailableCheckersView->setEnabled(true);
0077     m_ui->checkersToRunView->setEnabled(true);
0078 }
0079 
0080 //private:
0081 
0082 void SelectCheckersWidget::updateCheckers() {
0083     m_otherAvailableCheckersModel->setCheckers(m_otherAvailableCheckers);
0084     m_ui->otherAvailableCheckersView->expandAll();
0085 
0086     m_checkersToRunModel->setCheckers(m_checkersToRun);
0087     m_ui->checkersToRunView->expandAll();
0088 
0089     //It seems that the selection model is reset when the checker list is set in
0090     //the item model. When the selection model is reset, selectionChanged is not
0091     //emitted, so the buttons have to be explicitly disabled.
0092     m_ui->addButton->setEnabled(false);
0093     m_ui->removeButton->setEnabled(false);
0094 }
0095 
0096 //private slots:
0097 
0098 void SelectCheckersWidget::add() {
0099     QItemSelectionModel* selectionModel = m_ui->otherAvailableCheckersView->selectionModel();
0100     foreach (const QModelIndex& index, selectionModel->selection().indexes()) {
0101         const Checker* checker = m_otherAvailableCheckersModel->checkerForIndex(index);
0102         if (checker) {
0103             m_otherAvailableCheckers.removeOne(checker);
0104             m_checkersToRun.append(checker);
0105         }
0106     }
0107 
0108     updateCheckers();
0109 }
0110 
0111 void SelectCheckersWidget::remove() {
0112     QItemSelectionModel* selectionModel = m_ui->checkersToRunView->selectionModel();
0113     foreach (const QModelIndex& index, selectionModel->selection().indexes()) {
0114         const Checker* checker = m_checkersToRunModel->checkerForIndex(index);
0115         if (checker) {
0116             m_checkersToRun.removeOne(checker);
0117             m_otherAvailableCheckers.append(checker);
0118         }
0119     }
0120 
0121     updateCheckers();
0122 }
0123 
0124 void SelectCheckersWidget::handleOtherAvailableCheckersSelectionChanged() {
0125     bool noCheckerSelected = true;
0126     QItemSelectionModel* selectionModel = m_ui->otherAvailableCheckersView->selectionModel();
0127     foreach (const QModelIndex& index, selectionModel->selection().indexes()) {
0128         if (m_otherAvailableCheckersModel->checkerForIndex(index)) {
0129             noCheckerSelected = false;
0130         }
0131     }
0132 
0133     if (noCheckerSelected) {
0134         m_ui->addButton->setEnabled(false);
0135     } else {
0136         m_ui->addButton->setEnabled(true);
0137     }
0138 }
0139 
0140 void SelectCheckersWidget::handleCheckersToRunSelectionChanged() {
0141     bool noCheckerSelected = true;
0142     QItemSelectionModel* selectionModel = m_ui->checkersToRunView->selectionModel();
0143     foreach (const QModelIndex& index, selectionModel->selection().indexes()) {
0144         if (m_checkersToRunModel->checkerForIndex(index)) {
0145             noCheckerSelected = false;
0146         }
0147     }
0148 
0149     if (noCheckerSelected) {
0150         m_ui->removeButton->setEnabled(false);
0151     } else {
0152         m_ui->removeButton->setEnabled(true);
0153     }
0154 }