1 /**************************************************************************
2 *   Copyright (C) 2005-2020 by Oleksandr Shneyder                         *
3 *                              <o.shneyder@phoca-gmbh.de>                 *
4 *                                                                         *
5 *   This program 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 2 of the License, or     *
8 *   (at your option) any later version.                                   *
9 *   This program is distributed in the hope that it will be useful,       *
10 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 *   GNU General Public License for more details.                          *
13 *                                                                         *
14 *   You should have received a copy of the GNU General Public License     *
15 *   along with this program.  If not, see <https://www.gnu.org/licenses/>. *
16 ***************************************************************************/
17 #include "appdialog.h"
18 #include "onmainwindow.h"
19 
AppDialog(ONMainWindow * parent)20 AppDialog::AppDialog(ONMainWindow* parent):QDialog(parent)
21 {
22     setupUi(this);
23     mw=parent;
24 
25     media=0;
26     dev=0;
27     edu=0;
28     game=0;
29     graph=0;
30     net=0;
31     office=0;
32     set=0;
33     sys=0;
34     util=0;
35     other=0;
36     startButton->setEnabled(false);
37 
38     loadApps();
39 }
40 
~AppDialog()41 AppDialog::~AppDialog()
42 {
43 
44 }
45 
slotSearchChanged(QString text)46 void AppDialog::slotSearchChanged(QString text)
47 {
48     QTreeWidgetItemIterator it(treeWidget);
49     while (*it)
50     {
51         QString exec=(*it)->data(0,Qt::UserRole).toString();
52         QString comment=(*it)->data(0,Qt::UserRole+1).toString();
53         QString name=(*it)->text(0);
54         if ((*it)->childCount()==0)
55         {
56             if (text.length()<2)
57             {
58                 (*it)->setHidden(false);
59                 (*it)->setSelected(false);
60             }
61             else
62             {
63                 if (exec.indexOf(text, 0,Qt::CaseInsensitive)!= -1 ||
64                         comment.indexOf(text, 0,Qt::CaseInsensitive)!= -1 ||
65                         name.indexOf(text, 0,Qt::CaseInsensitive)!= -1 )
66                 {
67                     treeWidget->clearSelection();
68                     (*it)->setSelected(true);
69                     (*it)->setHidden(false);
70                     treeWidget->scrollToItem((*it));
71                 }
72                 else
73                 {
74                     (*it)->setHidden(true);
75                     (*it)->setSelected(false);
76                 }
77             }
78         }
79         ++it;
80     }
81 }
82 
initTopItem(QString text,QPixmap icon)83 QTreeWidgetItem* AppDialog::initTopItem(QString text, QPixmap icon)
84 {
85     QTreeWidgetItem* item;
86     item=new QTreeWidgetItem(treeWidget);
87     item->setText(0,text);
88     item->setFlags(Qt::ItemIsEnabled);
89     item->setIcon(0,icon);
90     return item;
91 }
92 
loadApps()93 void AppDialog::loadApps()
94 {
95     QTreeWidgetItem* parent = NULL;
96     foreach (Application app, mw->getApplications())
97     {
98         switch (app.category)
99         {
100         case Application::MULTIMEDIA:
101             if (!media)
102                 media=initTopItem(tr("Multimedia"), QPixmap(":/img/icons/22x22/applications-multimedia.png"));
103             parent=media;
104             break;
105         case Application::DEVELOPMENT:
106             if (!dev)
107                 dev=initTopItem(tr("Development"), QPixmap(":/img/icons/22x22/applications-development.png"));
108             parent=dev;
109             break;
110         case Application::EDUCATION:
111             if (!edu)
112                 edu=initTopItem(tr("Education"), QPixmap(":/img/icons/22x22/applications-education.png"));
113             parent=edu;
114             break;
115         case Application::GAME:
116             if (!game)
117                 game=initTopItem(tr("Game"), QPixmap(":/img/icons/22x22/applications-games.png"));
118             parent=game;
119             break;
120         case Application::GRAPHICS:
121             if (!graph)
122                 graph=initTopItem(tr("Graphics"), QPixmap(":/img/icons/22x22/applications-graphics.png"));
123             parent=graph;
124             break;
125         case Application::NETWORK:
126             if (!net)
127                 net=initTopItem(tr("Network"), QPixmap(":/img/icons/22x22/applications-internet.png"));
128             parent=net;
129             break;
130         case Application::OFFICE:
131             if (!office)
132                 office=initTopItem(tr("Office"), QPixmap(":/img/icons/22x22/applications-office.png"));
133             parent=office;
134             break;
135         case Application::SETTINGS:
136             if (!set)
137                 set=initTopItem(tr("Settings"), QPixmap(":/img/icons/22x22/preferences-system.png"));
138             parent=set;
139             break;
140         case Application::SYSTEM:
141             if (!sys)
142                 sys=initTopItem(tr("System"), QPixmap(":/img/icons/22x22/applications-system.png"));
143             parent=sys;
144             break;
145         case Application::UTILITY:
146             if (!util)
147                 util=initTopItem(tr("Utility"), QPixmap(":/img/icons/22x22/applications-utilities.png"));
148             parent=util;
149             break;
150         case Application::OTHER:
151             if (!other)
152                 other=initTopItem(tr("Other"), QPixmap(":/img/icons/22x22/applications-other.png"));
153             parent=other;
154             break;
155         default:
156             parent = NULL;
157             break;
158         }
159 
160         QTreeWidgetItem* it;
161         if (app.category==Application::TOP)
162             it=new QTreeWidgetItem(treeWidget);
163         else
164             it=new QTreeWidgetItem(parent);
165         it->setText(0, app.name);
166         it->setToolTip(0,app.comment);
167         it->setIcon(0,app.icon);
168         it->setData(0, Qt::UserRole, app.exec);
169         it->setData(0, Qt::UserRole+1, app.comment);
170     }
171     treeWidget->sortItems(0,Qt::AscendingOrder);
172 }
173 
slotSelectedChanged()174 void AppDialog::slotSelectedChanged()
175 {
176     startButton->setEnabled(false);
177     if (treeWidget->selectedItems().count())
178     {
179         startButton->setEnabled(true);
180     }
181 }
182 
slotDoubleClicked(QTreeWidgetItem * item)183 void AppDialog::slotDoubleClicked(QTreeWidgetItem* item)
184 {
185     QString exec=item->data(0,Qt::UserRole).toString();
186     if (exec.length()>0)
187         mw->runApplication(exec);
188 }
189 
slotStartSelected()190 void AppDialog::slotStartSelected()
191 {
192     if (treeWidget->selectedItems().count()>0)
193     {
194         QString exec=treeWidget->selectedItems()[0]->data(0,Qt::UserRole).toString();
195         if (exec.length()>0)
196             mw->runApplication(exec);
197     }
198 }
199