Developer Documentation
PluginDialog.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
45 
46 #include "PluginDialog.hh"
47 
48 #include <QMessageBox>
49 #include <QMenu>
50 
51 const QColor PluginDialog::blockColor_ = QColor(228, 155, 18);
52 const QColor PluginDialog::unloadColor_ = QColor(172, 172, 172);
53 const QColor PluginDialog::loadedBuiltInColor_ = QColor(208, 240, 192);
54 const QColor PluginDialog::failColor_ = Qt::red;
55 const QColor PluginDialog::loadedExternalColor_ = QColor(152, 255, 152);
56 
57 PluginDialog::PluginDialog(std::vector<PluginInfo>& _plugins, QWidget *parent)
58  : QDialog(parent),
59  plugins_(_plugins)
60 {
61  setupUi(this);
62 
63  list->setContextMenuPolicy(Qt::CustomContextMenu);
64 
65  connect(closeButton, SIGNAL(clicked()), this, SLOT(accept()));
66  connect(loadButton, SIGNAL(clicked()), this, SIGNAL(loadPlugin()));
67  connect(loadButton, SIGNAL(clicked()), this, SLOT(reject()));
68  connect(list,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(slotContextMenu(const QPoint&)));
69 
70  //set icons
71  QString iconPath = OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator();
72 
73  closeButton->setIcon( QIcon(iconPath + "window-close.png"));
74  loadButton->setIcon( QIcon(iconPath + "network-connect.png"));
75 
76 }
77 
78 void PluginDialog::closeEvent(QCloseEvent *event)
79 {
80  event->accept();
81  accept();
82 }
83 
84 int PluginDialog::exec()
85 {
86 
87  for (uint i = 0; i < plugins_.size(); i++){
88  QFrame* frame = new QFrame();
89  QHBoxLayout* hlayout = new QHBoxLayout;
90  QLabel* name = new QLabel( plugins_[i].name );
91  QFont font;
92  font.setBold(true);
93  font.setPointSize(10);
94  name->setFont(font);
95  QLabel* version = new QLabel( plugins_[i].version );
96 // QLabel* author = new QLabel( "RWTH Computer Graphics Group" );
97 
98  hlayout->addWidget(name);
99  hlayout->addStretch();
100  hlayout->addWidget(version);
101 // hlayout->addSpacing(10);
102 // hlayout->addWidget(author);
103 
104  QVBoxLayout* vlayout = new QVBoxLayout;
105 
106  QLabel* description = new QLabel( plugins_[i].description );
107  descriptions_.push_back(description);
108 
109  vlayout->addLayout(hlayout,20);
110  vlayout->addWidget(description);
111  frame->setLayout(vlayout);
112 
113  QListWidgetItem *item = new QListWidgetItem("");
114  frames_.push_back(frame);
115  item->setSizeHint( QSize (100,50) );
116 
117  //set color depending on the current status
118  switch(plugins_[i].status)
119  {
120  case PluginInfo::LOADED:
121  if (plugins_[i].buildIn)
122  item->setBackground(loadedBuiltInColor_);
123  else
124  {
125  item->setBackground(loadedExternalColor_);
126  description->setText(description->text()+tr(" *EXTERNAL*"));
127  }
128  break;
129  case PluginInfo::FAILED:
130  item->setBackground(failColor_);
131  description->setText(description->text()+tr(" *FAILED*"));
132  break;
133  case PluginInfo::BLOCKED:
134  item->setBackground(blockColor_);
135  description->setText(description->text()+tr(" *BLOCKED*"));
136  break;
137  case PluginInfo::UNLOADED:
138  item->setBackground(unloadColor_);
139  description->setText(description->text()+tr(" *UNLOADED*"));
140  break;
141  }
142  list->addItem(item);
143  list->setItemWidget(item, frame);
144  }
145 
146  int ret = QDialog::exec();
147 
148  for (int i=0; i < frames_.count(); i++)
149  delete frames_[i];
150 
151  return ret;
152 }
153 
154 void PluginDialog::slotBlockPlugin()
155 {
156  for (int i=0; i < list->selectedItems().size(); ++i)
157  {
158  QListWidgetItem* widget = list->selectedItems()[i];
159  widget->setBackground(blockColor_);
160 
161  PluginInfo* plugin = &plugins_[ list->row( widget ) ];
162  descriptions_[list->row( widget )]->setText(plugin->description + tr(" *BLOCKED*"));
163 
164  emit blockPlugin(plugin->name);
165  }
166 }
167 void PluginDialog::slotUnBlockPlugin()
168 {
169  for (int i=0; i < list->selectedItems().size(); ++i)
170  {
171  QListWidgetItem* widget = list->selectedItems()[i];
172  widget->setBackground(unloadColor_);
173 
174  PluginInfo* plugin = &plugins_[ list->row( widget ) ];
175  descriptions_[list->row( widget )]->setText(plugin->description);
176 
177  emit unBlockPlugin(plugin->name);
178  }
179 }
180 
181 void PluginDialog::slotLoadPlugin()
182 {
183  for (int i=0; i < list->selectedItems().size(); ++i)
184  {
185  QListWidgetItem* widget = list->selectedItems()[i];
186 
187  PluginInfo* plugin = &plugins_[ list->row( widget ) ];
188 
189  if (plugin->status == PluginInfo::BLOCKED)
190  {
191  QMessageBox msgBox;
192  msgBox.setText("Plugin is blocked. Unblock it?");
193  msgBox.setWindowTitle("Plugin blocked");
194  msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
195  msgBox.setDefaultButton(QMessageBox::Yes);
196  int rep = msgBox.exec();
197  if ( rep == QMessageBox::Yes)
198  emit unBlockPlugin(plugin->name);
199  }
200 
201  if (plugin->buildIn)
202  widget->setBackground(loadedBuiltInColor_);
203  else
204  widget->setBackground(loadedExternalColor_);
205 
206  descriptions_[list->row( widget )]->setText(plugin->description);
207 
208  QString licenseErros;
209  emit loadPlugin(plugin->path,false,licenseErros,plugin->plugin);
210 
211  if (plugin->status == PluginInfo::FAILED)
212  {
213  descriptions_[list->row( widget )]->setText(plugin->description + tr(" *FAILED*"));
214  widget->setBackground(failColor_);
215  }else
216  {
217  plugin->status = PluginInfo::LOADED;
218  }
219  }
220 }
221 
222 void PluginDialog::slotContextMenu(const QPoint& _point)
223 {
224  if (!list->count())
225  return;
226 
227  QMenu *menu = new QMenu(list);
228  QAction* action = 0;
229 
230  PluginInfo* plugin = &plugins_[list->currentRow()];
231 
232  if ( plugin->status != PluginInfo::BLOCKED)//not blocked
233  {
234  action = menu->addAction(tr("Block Plugin"));
235  connect(action,SIGNAL(triggered(bool)),this,SLOT(slotBlockPlugin()));
236  }else//blocked
237  {
238  action = menu->addAction(tr("Unblock Plugin"));
239  connect(action,SIGNAL(triggered(bool)),this,SLOT(slotUnBlockPlugin()));
240  }
241 
242  if ( plugin->status != PluginInfo::LOADED)
243  {
244  action = menu->addAction(tr("Load Plugin"));
245  connect(action,SIGNAL(triggered(bool)),this,SLOT(slotLoadPlugin()));
246  }
247 
248  menu->exec(list->mapToGlobal(_point),0);
249 
250 }
QString description
Description of the plugin ( requested from the plugin on load)
Definition: PluginInfo.hh:131
QString name
Name of the plugin ( requested from the plugin on load)
Definition: PluginInfo.hh:128
bool buildIn
Indicates, if the plugin is a built in Plugin (in Plugin directory)
Definition: PluginInfo.hh:176
QString path
Path to the plugin ( set on load )
Definition: PluginInfo.hh:137
QObject * plugin
Pointer to the loaded plugin (Already casted when loading it)
Definition: PluginInfo.hh:125