viewModeWidget.cc 11.3 KB
Newer Older
1
2
3
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
39
40
41
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                         *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61




#include "viewModeWidget.hh"
#include <OpenFlipper/widgets/coreWidget/CoreWidget.hh>
#include <OpenFlipper/common/GlobalOptions.hh>

#include <QInputDialog>
#include <QMessageBox>

/// Constructor
viewModeWidget::viewModeWidget(const QVector< ViewMode* >& _modes, QWidget *_parent)
  : QDialog(_parent),
    modes_(_modes)
{
  setupUi(this);



62
  connect(viewModeList ,SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
Jan Möbius's avatar
 
Jan Möbius committed
63
          this,SLOT(slotModeContextMenu ( const QPoint & ) ));
64
  connect(toolboxList ,SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
Jan Möbius's avatar
 
Jan Möbius committed
65
66
          this,SLOT(slotToolContextMenu ( const QPoint & ) ));

67
68
  viewModeList->setContextMenuPolicy(Qt::CustomContextMenu);
  toolboxList->setContextMenuPolicy(Qt::CustomContextMenu);
Jan Möbius's avatar
 
Jan Möbius committed
69

70
  connect(viewModeList, SIGNAL(itemSelectionChanged()), this, SLOT(slotSetToolWidgets()));
Jan Möbius's avatar
 
Jan Möbius committed
71
72
73
74
75

  connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
  connect(okButton, SIGNAL(clicked()), this, SLOT(slotChangeView()));
  connect(saveButton, SIGNAL(clicked()), this, SLOT(slotSaveMode()));
  connect(removeButton, SIGNAL(clicked()), this, SLOT(slotRemoveMode()));
76
77
  connect(viewModeList, SIGNAL(currentTextChanged (QString)), this, SLOT(slotModeChanged(QString)) );
  connect(viewModeList, SIGNAL(clicked (QModelIndex)), this, SLOT(slotModeClicked(QModelIndex)) );
Jan Möbius's avatar
 
Jan Möbius committed
78
79
80
81
82
83
84
85
86
87
88
89
  connect(upButton, SIGNAL(clicked()), this, SLOT(slotMoveUp()) );
  connect(downButton, SIGNAL(clicked()), this, SLOT(slotMoveDown()) );
  connect(removeWidgetButton, SIGNAL(clicked()), this, SLOT(slotRemoveWidget()) );

  // load icons for tool buttons
  upButton->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "arrow-up.png" ) );
  removeWidgetButton->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "edit-delete.png" ) );
  downButton->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "arrow-down.png" ) );
}

/// Move Widget up in the list
void viewModeWidget::slotMoveUp(){
90
91
  if (toolboxList->selectedItems().count() == 1){
    if (toolboxList->currentRow() == 0) return;
Jan Möbius's avatar
 
Jan Möbius committed
92
    //extract a list of all items
93
94
    QString item = toolboxList->currentItem()->text();
    int oldRow = toolboxList->currentRow();
Jan Möbius's avatar
 
Jan Möbius committed
95
    QStringList widgets;
96
97
    for (int i=0; i < toolboxList->count(); i++)
      widgets << toolboxList->item(i)->text();
Jan Möbius's avatar
 
Jan Möbius committed
98
99
100
101
102
103
104
105
106
107

    //reorder items
    QString last = widgets[0];
    for (int i=1; i < widgets.size(); i++){
      if (widgets[i] == item){
        widgets[i] = last;
        widgets[i-1] = item;
      }
      last = widgets[i];
    }
108
109
110
    // set new list as elements for the toolboxList
    toolboxList->clear();
    toolboxList->addItems(widgets);
Jan Möbius's avatar
 
Jan Möbius committed
111
    //highlight active item
112
    toolboxList->setCurrentRow(oldRow-1);
Jan Möbius's avatar
 
Jan Möbius committed
113
114
115
116
117
  }
}

/// Move widget do in the list
void viewModeWidget::slotMoveDown(){
118
119
  if (toolboxList->selectedItems().count() == 1){
    if (toolboxList->currentRow() == toolboxList->count()-1) return;
Jan Möbius's avatar
 
Jan Möbius committed
120
    //extract a list of all items
121
122
    QString item = toolboxList->currentItem()->text();
    int oldRow = toolboxList->currentRow();
Jan Möbius's avatar
 
Jan Möbius committed
123
    QStringList widgets;
124
125
    for (int i=0; i < toolboxList->count(); i++)
      widgets << toolboxList->item(i)->text();
Jan Möbius's avatar
 
Jan Möbius committed
126
127
128
129
130
131
132
133
134
135

    //reorder items
    QString last = widgets[widgets.size()-1];
    for (int i=widgets.size()-2; i >= 0; i--){
      if (widgets[i] == item){
        widgets[i] = last;
        widgets[i+1] = item;
      }
      last = widgets[i];
    }
136
137
138
    // set new list as elements for the toolboxList
    toolboxList->clear();
    toolboxList->addItems(widgets);
Jan Möbius's avatar
 
Jan Möbius committed
139
    //highlight active item
140
    toolboxList->setCurrentRow(oldRow+1);
Jan Möbius's avatar
 
Jan Möbius committed
141
142
143
144
145
  }
}

/// remove Widget form the list
void viewModeWidget::slotRemoveWidget(){
146
  qDeleteAll(toolboxList->selectedItems());
Jan Möbius's avatar
 
Jan Möbius committed
147
148
149
}

/// Slot for updating removeButton when new mode is selected
Dirk Wilden's avatar
Dirk Wilden committed
150
void viewModeWidget::slotModeClicked(QModelIndex /*_id*/){
Jan Möbius's avatar
 
Jan Möbius committed
151
152
153
154
  slotModeChanged(QString());
}

/// Slot for updating removeButton when new mode is selected
Dirk Wilden's avatar
Dirk Wilden committed
155
void viewModeWidget::slotModeChanged(QString /*_mode*/){
Jan Möbius's avatar
 
Jan Möbius committed
156
  //check if mode is custom e.g. that it can be removed
157
  if (viewModeList->selectedItems().count() > 0){
Jan Möbius's avatar
 
Jan Möbius committed
158
    for (int i=0; i < modes_.size(); i++)
159
      if (modes_[i]->name == viewModeList->currentItem()->text()){
Jan Möbius's avatar
 
Jan Möbius committed
160
161
162
163
164
165
166
167
168
        removeButton->setEnabled(modes_[i]->custom);
        break;
      }
  }
}

/// opens custom context menu in Mode-List
void viewModeWidget::slotModeContextMenu ( const QPoint & _pos ){

169
  if (viewModeList->itemAt(_pos)){
Jan Möbius's avatar
 
Jan Möbius committed
170
171
172
173
174
175
176

    QMenu menu(0);

    menu.addAction(tr("Remove Mode"), this, SLOT ( slotRemoveMode() ));

    //check if mode is custom e.g. that it can be removed
    for (int i=0; i < modes_.size(); i++)
177
      if (modes_[i]->name == viewModeList->currentItem()->text()){
Jan Möbius's avatar
 
Jan Möbius committed
178
179
180
181
          menu.actions()[0]->setEnabled(modes_[i]->custom);
        break;
      }

182
    menu.exec(viewModeList->mapToGlobal( _pos) );
Jan Möbius's avatar
 
Jan Möbius committed
183
184
185
186
187
188
  }
}

/// opens custom context menu in tool-List
void viewModeWidget::slotToolContextMenu ( const QPoint & _pos ){

189
  if (toolboxList->itemAt(_pos)){
Jan Möbius's avatar
 
Jan Möbius committed
190
191
192

    QMenu menu(0);

193
    if (toolboxList->selectedItems().count() == 1){
Jan Möbius's avatar
 
Jan Möbius committed
194
195
196
197
198
199
200
      menu.addAction(tr("Move up"), this, SLOT ( slotMoveUp() ));
      menu.addAction(tr("Move down"), this, SLOT ( slotMoveDown() ));
      menu.addSeparator();
    }

    menu.addAction(tr("Remove Widget"), this, SLOT ( slotRemoveWidget() ));

201
    menu.exec(toolboxList->mapToGlobal( _pos) );
Jan Möbius's avatar
 
Jan Möbius committed
202
203
204
205
206
  }
}

/// Slot for removing custom modes
void viewModeWidget::slotRemoveMode(){
207
208
  emit removeMode( viewModeList->currentItem()->text() );
  QListWidgetItem* item =  viewModeList->takeItem( viewModeList->currentRow() );
Jan Möbius's avatar
 
Jan Möbius committed
209
210
211
212
213
  delete item;
}

/// Slot for updating ToolWidget-List depending on the selected Mode
void viewModeWidget::slotSetToolWidgets(){
214
  toolboxList->clear();
Jan Möbius's avatar
 
Jan Möbius committed
215
  //iterate over all selected modes
216
  for (int m=0; m < viewModeList->selectedItems().size(); m++)
Jan Möbius's avatar
 
Jan Möbius committed
217
218
    // find mode in modeVector modes_
    for (int i=0; i < modes_.size(); i++)
219
220
      if ( modes_[i]->name == (viewModeList->selectedItems()[m])->text() )
        toolboxList->addItems(modes_[i]->visibleToolboxes); //add corresponding widgets
Jan Möbius's avatar
 
Jan Möbius committed
221
222
223
224
225
226
}

/// Slot for changing View and closing widget
void viewModeWidget::slotChangeView(){
  //get widgets
  QStringList widgets;
227
228
  for (int i=0; i < toolboxList->count(); i++)
    widgets << toolboxList->item(i)->text();
Jan Möbius's avatar
 
Jan Möbius committed
229
  //get mode
230
  QString mode = "";
231
232
  if (viewModeList->selectedItems().size() > 0)
    mode = viewModeList->selectedItems()[0]->text();
Jan Möbius's avatar
 
Jan Möbius committed
233
234
235
236
237
238
  emit changeView(mode,widgets);
  close();
}

/// Slot for saving current List of Widgets as custom mode
void viewModeWidget::slotSaveMode(){
239
  if (toolboxList->count() == 0) return;
Jan Möbius's avatar
 
Jan Möbius committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  //ask for a name for the new viewmode
  bool ok;
  QString name = QInputDialog::getText(this, tr("Change View Mode"),
                                       tr("Enter a name for the new ViewMode:"), QLineEdit::Normal,
                                          "", &ok);
  //Remove Spaces from name
  name.remove(" ");

  if (!ok || name.isEmpty()) return;

  //check if name already exists
  for (int i=0; i < modes_.size(); i++)
    if (modes_[i]->name == name){
      QMessageBox::warning(this, tr("Change View Mode"), tr("Cannot Save ViewMode. Name already taken by a different mode."),
                                        QMessageBox::Ok);
      return;
    }

  QStringList widgets;
259
260
  for (int i=0; i < toolboxList->count(); i++)
    widgets << toolboxList->item(i)->text();
Jan Möbius's avatar
 
Jan Möbius committed
261
262
263
264
265
266
267
268
  emit saveMode(name, true, widgets);
  show(name);
}

/// overloaded show function
void viewModeWidget::show(QString _lastMode){
  QDialog::show();

269
270
  //fill viewModeList
  viewModeList->clear();
Jan Möbius's avatar
 
Jan Möbius committed
271
  for (int i=0; i < modes_.size(); i++){
272
273
274
275
276
277
    QListWidgetItem *item = new QListWidgetItem(viewModeList);
    item->setTextAlignment(Qt::AlignHCenter);
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    item->setIcon(QIcon(OpenFlipper::Options::iconDirStr() + QDir::separator () + "Unknown.png"));
    item->setText(modes_[i]->name);

Jan Möbius's avatar
 
Jan Möbius committed
278
    if (modes_[i]->custom)
279
      viewModeList->item(i)->setForeground( QBrush(QColor(0,0,150) ) );
Jan Möbius's avatar
 
Jan Möbius committed
280
    else
281
      viewModeList->item(i)->setForeground( QBrush(QColor(0,0,0) ) );
Jan Möbius's avatar
 
Jan Möbius committed
282
283
284
  }

  //select given mode
285
  viewModeList->setCurrentRow(0);
Jan Möbius's avatar
 
Jan Möbius committed
286

287
288
289
  for (int i=0; i < viewModeList->count(); i++)
    if (viewModeList->item(i)->text() == _lastMode)
      viewModeList->setCurrentRow(i);
Jan Möbius's avatar
 
Jan Möbius committed
290
291
292
293

  removeButton->setEnabled(false);
}