1 /*
2 	Actiona
3 	Copyright (C) 2005 Jonathan Mercier-Ganady
4 
5 	Actiona is free software: you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation, either version 3 of the License, or
8 	(at your option) any later version.
9 
10 	Actiona is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 	Contact : jmgr@jmgr.info
19 */
20 
21 #include "newactiondialog.h"
22 #include "ui_newactiondialog.h"
23 #include "actionfactory.h"
24 #include "actiondefinition.h"
25 #include "newactionmodel.h"
26 #include "newactionproxymodel.h"
27 
28 #include <QPushButton>
29 
NewActionDialog(ActionTools::ActionFactory * actionFactory,NewActionModel * newActionModel,QWidget * parent)30 NewActionDialog::NewActionDialog(ActionTools::ActionFactory *actionFactory,
31                                  NewActionModel *newActionModel,
32                                  QWidget *parent)
33 	: QDialog(parent),
34 	ui(new Ui::NewActionDialog),
35     mActionFactory(actionFactory),
36     mNewActionModel(newActionModel),
37     mNewActionProxyModel(new NewActionProxyModel(this))
38 {
39 	ui->setupUi(this);
40 
41     mNewActionProxyModel->setDynamicSortFilter(false);
42 
43     ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
44 
45     {
46         QItemSelectionModel *model = ui->newActionTreeView->selectionModel();
47         mNewActionProxyModel->setSourceModel(mNewActionModel);
48         ui->newActionTreeView->setModel(mNewActionProxyModel);
49         delete model;
50     }
51 
52     QItemSelectionModel *selectionModel = ui->newActionTreeView->selectionModel();
53 
54     connect(selectionModel, &QItemSelectionModel::currentChanged, this, &NewActionDialog::onCurrentChanged);
55 
56     ui->newActionTreeView->expandAll();
57 }
58 
~NewActionDialog()59 NewActionDialog::~NewActionDialog()
60 {
61 	delete ui;
62 }
63 
exec()64 int NewActionDialog::exec()
65 {
66     QStandardItem *selectedItem = nullptr;
67 
68     for(int rowIndex = 0; rowIndex < mNewActionModel->rowCount(); ++rowIndex)
69     {
70         QStandardItem *currentItem = mNewActionModel->item(rowIndex);
71         if(!currentItem)
72             continue;
73 
74         if(currentItem->hasChildren())
75         {
76             selectedItem = currentItem->child(0);
77             break;
78         }
79     }
80 
81     if(selectedItem)
82         ui->newActionTreeView->setCurrentIndex(mNewActionProxyModel->mapFromSource(selectedItem->index()));
83 
84 	return QDialog::exec();
85 }
86 
on_newActionTreeView_doubleClicked(const QModelIndex & index)87 void NewActionDialog::on_newActionTreeView_doubleClicked(const QModelIndex &index)
88 {
89     Q_UNUSED(index)
90 
91     accept();
92 }
93 
on_filterLineEdit_textChanged(const QString & text)94 void NewActionDialog::on_filterLineEdit_textChanged(const QString &text)
95 {
96     mNewActionProxyModel->setFilterString(text);
97     ui->newActionTreeView->expandAll();
98 }
99 
onCurrentChanged(const QModelIndex & current,const QModelIndex & previous)100 void NewActionDialog::onCurrentChanged(const QModelIndex &current, const QModelIndex &previous)
101 {
102     Q_UNUSED(previous)
103 
104     ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
105 
106     auto index = mNewActionProxyModel->mapToSource(current);
107     const QString &actionId = index.data(NewActionModel::ActionIdRole).toString();
108     ActionTools::ActionDefinition *actionDefinition = mActionFactory->actionDefinition(actionId);
109     if(!actionDefinition)
110         return;
111 
112     ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
113 
114     ui->actionDescription->setText(actionDefinition->description());
115     ui->versionLabel->setText(actionDefinition->version().toString());
116 
117     QString status;
118     switch(actionDefinition->status())
119     {
120     case ActionTools::Alpha:
121         status = tr("Alpha");
122         break;
123     case ActionTools::Beta:
124         status = tr("Beta");
125         break;
126     case ActionTools::Testing:
127         status = tr("Testing");
128         break;
129     case ActionTools::Stable:
130         status = tr("Stable");
131         break;
132     }
133     ui->statusLabel->setText(status);
134 
135     QString official;
136     if(actionDefinition->flags() & ActionTools::Official)
137         official = tr("Yes");
138     else
139         official = tr("No");
140     ui->officialLabel->setText(official);
141 }
142 
accept()143 void NewActionDialog::accept()
144 {
145     auto selectedIndexes = ui->newActionTreeView->selectionModel()->selectedRows(0);
146     if(selectedIndexes.isEmpty())
147         return;
148 
149     auto firstIndex = mNewActionProxyModel->mapToSource(selectedIndexes.first());
150 
151     QString selectedAction = firstIndex.data(NewActionModel::ActionIdRole).toString();
152 
153     if(selectedAction.isEmpty())
154         return;
155 
156     mSelectedAction = selectedAction;
157 
158 	QDialog::accept();
159 }
160