PluginDialog.cc 8.99 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
*                                                                            *
40
41
\*===========================================================================*/

42

43
44
#include <OpenFlipper/BasePlugin/BaseInterface.hh>
#include <OpenFlipper/common/GlobalOptions.hh>
45

Dirk Wilden's avatar
Dirk Wilden committed
46
#include "PluginDialog.hh"
Matthias Möller's avatar
Matthias Möller committed
47

48
49
50
#include <QMessageBox>
#include <QMenu>

51
52
53
54
55
const QColor PluginDialog::blockColor_  = QColor(228, 155, 18);
const QColor PluginDialog::unloadColor_ = QColor(172, 172, 172);
const QColor PluginDialog::loadedBuiltInColor_ = QColor(208, 240, 192);
const QColor PluginDialog::failColor_ = Qt::red;
const QColor PluginDialog::loadedExternalColor_ = QColor(152, 255, 152);
Dirk Wilden's avatar
Dirk Wilden committed
56
57
58
59
60
61
62

PluginDialog::PluginDialog(std::vector<PluginInfo>& _plugins, QWidget *parent)
    : QDialog(parent),
      plugins_(_plugins)
{
  setupUi(this);

63
64
  list->setContextMenuPolicy(Qt::CustomContextMenu);

Dirk Wilden's avatar
Dirk Wilden committed
65
66
67
  connect(closeButton, SIGNAL(clicked()), this, SLOT(accept()));
  connect(loadButton, SIGNAL(clicked()), this, SIGNAL(loadPlugin()));
  connect(loadButton, SIGNAL(clicked()), this, SLOT(reject()));
68
  connect(list,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(slotContextMenu(const QPoint&)));
Dirk Wilden's avatar
Dirk Wilden committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

  //set icons
  QString iconPath = OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator();

  closeButton->setIcon( QIcon(iconPath + "window-close.png"));
  loadButton->setIcon( QIcon(iconPath + "network-connect.png"));

}

void PluginDialog::closeEvent(QCloseEvent *event)
{
  event->accept();
  accept();
}

int PluginDialog::exec()
{

  for (uint i = 0; i < plugins_.size(); i++){
    QFrame* frame = new QFrame();
    QHBoxLayout* hlayout = new QHBoxLayout;
    QLabel* name = new QLabel( plugins_[i].name );
    QFont font;
    font.setBold(true);
    font.setPointSize(10);
    name->setFont(font);
    QLabel* version = new QLabel( plugins_[i].version );
//     QLabel* author = new QLabel( "RWTH Computer Graphics Group" );

    hlayout->addWidget(name);
    hlayout->addStretch();
    hlayout->addWidget(version);
//     hlayout->addSpacing(10);
//     hlayout->addWidget(author);

    QVBoxLayout* vlayout = new QVBoxLayout;

    QLabel* description = new QLabel( plugins_[i].description );
107
    descriptions_.push_back(description);
Dirk Wilden's avatar
Dirk Wilden committed
108
109
110
111
112
113
114
115

    vlayout->addLayout(hlayout,20);
    vlayout->addWidget(description);
    frame->setLayout(vlayout);

    QListWidgetItem *item = new QListWidgetItem("");
    frames_.push_back(frame);
    item->setSizeHint( QSize (100,50) );
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

    //set color depending on the current status
    switch(plugins_[i].status)
    {
      case PluginInfo::LOADED:
        if (plugins_[i].buildIn)
          item->setBackground(loadedBuiltInColor_);
        else
        {
          item->setBackground(loadedExternalColor_);
          description->setText(description->text()+tr(" *EXTERNAL*"));
        }
        break;
      case PluginInfo::FAILED:
        item->setBackground(failColor_);
        description->setText(description->text()+tr(" *FAILED*"));
        break;
      case PluginInfo::BLOCKED:
        item->setBackground(blockColor_);
        description->setText(description->text()+tr(" *BLOCKED*"));
        break;
      case PluginInfo::UNLOADED:
        item->setBackground(unloadColor_);
        description->setText(description->text()+tr(" *UNLOADED*"));
        break;
    }
Dirk Wilden's avatar
Dirk Wilden committed
142
143
144
145
146
147
148
149
150
151
152
153
    list->addItem(item);
    list->setItemWidget(item, frame);
  }

  int ret = QDialog::exec();

  for (int i=0; i < frames_.count(); i++)
    delete frames_[i];

  return ret;
}

154
155
156
157
158
159
160
161
162
163
164
165
166
167
void PluginDialog::slotBlockPlugin()
{
  for (int i=0; i < list->selectedItems().size(); ++i)
  {
    QListWidgetItem* widget = list->selectedItems()[i];
    widget->setBackground(blockColor_);

    PluginInfo* plugin = &plugins_[ list->row( widget ) ];
    descriptions_[list->row( widget )]->setText(plugin->description + tr(" *BLOCKED*"));

    emit blockPlugin(plugin->name);
  }
}
void PluginDialog::slotUnBlockPlugin()
Dirk Wilden's avatar
Dirk Wilden committed
168
{
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  for (int i=0; i < list->selectedItems().size(); ++i)
  {
    QListWidgetItem* widget = list->selectedItems()[i];
    widget->setBackground(unloadColor_);

    PluginInfo* plugin = &plugins_[ list->row( widget ) ];
    descriptions_[list->row( widget )]->setText(plugin->description);

    emit unBlockPlugin(plugin->name);
  }
}

void PluginDialog::slotLoadPlugin()
{
  for (int i=0; i < list->selectedItems().size(); ++i)
  {
    QListWidgetItem* widget = list->selectedItems()[i];

    PluginInfo* plugin = &plugins_[ list->row( widget ) ];

    if (plugin->status == PluginInfo::BLOCKED)
    {
      QMessageBox msgBox;
      msgBox.setText("Plugin is blocked. Unblock it?");
      msgBox.setWindowTitle("Plugin blocked");
      msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
      msgBox.setDefaultButton(QMessageBox::Yes);
      int rep = msgBox.exec();
      if ( rep == QMessageBox::Yes)
        emit unBlockPlugin(plugin->name);
Dirk Wilden's avatar
Dirk Wilden committed
199
200
    }

201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
    if (plugin->buildIn)
      widget->setBackground(loadedBuiltInColor_);
    else
      widget->setBackground(loadedExternalColor_);

    descriptions_[list->row( widget )]->setText(plugin->description);

    QString licenseErros;
    emit loadPlugin(plugin->path,false,licenseErros,plugin->plugin);

    if (plugin->status == PluginInfo::FAILED)
    {
      descriptions_[list->row( widget )]->setText(plugin->description + tr(" *FAILED*"));
      widget->setBackground(failColor_);
    }else
    {
      plugin->status = PluginInfo::LOADED;
218
    }
219
220
  }
}
Dirk Wilden's avatar
Dirk Wilden committed
221

222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
void PluginDialog::slotContextMenu(const QPoint& _point)
{
  if (!list->count())
    return;

  QMenu *menu = new QMenu(list);
  QAction* action = 0;

  PluginInfo* plugin = &plugins_[list->currentRow()];

  if ( plugin->status != PluginInfo::BLOCKED)//not blocked
  {
    action = menu->addAction(tr("Block Plugin"));
    connect(action,SIGNAL(triggered(bool)),this,SLOT(slotBlockPlugin()));
  }else//blocked
  {
    action = menu->addAction(tr("Unblock Plugin"));
    connect(action,SIGNAL(triggered(bool)),this,SLOT(slotUnBlockPlugin()));
Dirk Wilden's avatar
Dirk Wilden committed
240
241
  }

242
243
244
245
246
247
248
249
  if ( plugin->status != PluginInfo::LOADED)
  {
    action = menu->addAction(tr("Load Plugin"));
    connect(action,SIGNAL(triggered(bool)),this,SLOT(slotLoadPlugin()));
  }

  menu->exec(list->mapToGlobal(_point),0);

Dirk Wilden's avatar
Dirk Wilden committed
250
}