viewModeWidget.cc 32.7 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
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
*                           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/>.                                       *
*                                                                            *
33 34 35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36 37 38 39 40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
\*===========================================================================*/
42 43 44 45 46 47 48 49 50




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

#include <QInputDialog>
51
#include <QFileDialog>
52 53 54 55 56 57 58 59 60 61
#include <QMessageBox>

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


Jan Möbius's avatar
Jan Möbius committed
62
  connect(viewModeList, SIGNAL(itemSelectionChanged()), this, SLOT(slotSetAllWidgets()));
63

64
  
Jan Möbius's avatar
Jan Möbius committed
65 66 67
  
  
  
68 69
  connect(viewModeList, SIGNAL(currentTextChanged (QString)), this, SLOT(slotModeChanged(QString)) );
  connect(viewModeList, SIGNAL(clicked (QModelIndex)), this, SLOT(slotModeClicked(QModelIndex)) );
70

71 72
  
  
Jan Möbius's avatar
Jan Möbius committed
73 74 75 76
  // View Mode buttons
  connect(removeButton, SIGNAL(clicked()), this, SLOT(slotRemoveMode()));
  connect(copyButton, SIGNAL(clicked()), this, SLOT(slotCopyMode()));
  connect(addButton, SIGNAL(clicked()), this, SLOT(slotAddMode()));
77 78 79 80 81 82 83
  
  // View Mode List context Menu
  viewModeList->setContextMenuPolicy(Qt::CustomContextMenu);
  connect(viewModeList ,SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
          this         ,SLOT(slotModeContextMenu ( const QPoint & ) ));
  
  
Jan Möbius's avatar
Jan Möbius committed
84
  // Context Menus for Toolbars
85 86 87 88 89 90 91 92
  toolbarList->setContextMenuPolicy(Qt::CustomContextMenu);
  availableToolbars->setContextMenuPolicy(Qt::CustomContextMenu);
  connect(toolbarList ,SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
          this        ,SLOT(slotUsedToolbarContextMenu ( const QPoint & ) )); 
  connect(availableToolbars ,SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
          this        ,SLOT(slotAvailableToolbarContextMenu ( const QPoint & ) )); 
  
  
Jan Möbius's avatar
Jan Möbius committed
93
  // Context Menus for Toolboxes
94 95 96 97 98
  toolboxList->setContextMenuPolicy(Qt::CustomContextMenu);
  availableToolboxes->setContextMenuPolicy(Qt::CustomContextMenu);
  connect(toolboxList ,SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
          this        ,SLOT(slotUsedToolboxContextMenu ( const QPoint & ) )); 
  connect(availableToolboxes ,SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
Jan Möbius's avatar
Jan Möbius committed
99 100 101 102 103 104 105 106 107 108
          this        ,SLOT(slotAvailableToolboxContextMenu ( const QPoint & ) ));


  // Context Menus for Context Menus
  contextMenuList->setContextMenuPolicy(Qt::CustomContextMenu);
  availableContextMenus->setContextMenuPolicy(Qt::CustomContextMenu);
  connect(contextMenuList ,SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
          this        ,SLOT(slotUsedContextMenuContextMenu ( const QPoint & ) )); 
  connect(availableContextMenus ,SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
          this        ,SLOT(slotAvailableContextMenuContextMenu ( const QPoint & ) )); 
109 110
          
  
Jan Möbius's avatar
Jan Möbius committed
111
  // Toolbar Buttons to add and remove toolbars in the given Mode
112 113 114 115 116 117
  connect(rightArrowToolbar, SIGNAL(clicked()), this, SLOT(slotRightArrowToolbar()) );
  rightArrowToolbar->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "arrow-right.png" ) );
  connect(leftArrowToolbar, SIGNAL(clicked()), this, SLOT(slotLeftArrowToolbar()) );
  leftArrowToolbar->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "arrow-left.png" ) );
  
  
Jan Möbius's avatar
Jan Möbius committed
118
  // Toolbox Buttons to add, remove and order toolboxes in the given Mode
119 120 121 122 123
  connect(rightArrowToolbox, SIGNAL(clicked()), this, SLOT(slotRightArrowToolbox()) );
  rightArrowToolbox->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "arrow-right.png" ) );
  connect(leftArrowToolbox, SIGNAL(clicked()), this, SLOT(slotLeftArrowToolbox()) );
  leftArrowToolbox->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "arrow-left.png" ) );  
  
Jan Möbius's avatar
Jan Möbius committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  connect(upButtonToolbox, SIGNAL(clicked()), this, SLOT(slotMoveToolboxUp()) );
  upButtonToolbox->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "arrow-up.png" ) );
  connect(downButtonToolbox, SIGNAL(clicked()), this, SLOT(slotMoveToolboxDown()) );
  downButtonToolbox->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "arrow-down.png" ) );
  
  
  // ContextMenu Buttons to add, remove and order ContextMenus in the given Mode
  connect(rightArrowContextMenu, SIGNAL(clicked()), this, SLOT(slotRightArrowContextMenu()) );
  rightArrowContextMenu->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "arrow-right.png" ) );
  connect(leftArrowContextMenu, SIGNAL(clicked()), this, SLOT(slotLeftArrowContextMenu()) );
  leftArrowContextMenu->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "arrow-left.png" ) );  
  
  connect(upButtonContextMenu, SIGNAL(clicked()), this, SLOT(slotMoveContextMenuUp()) );
  upButtonContextMenu->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "arrow-up.png" ) );
  connect(downButtonContextMenu, SIGNAL(clicked()), this, SLOT(slotMoveContextMenuDown()) );
  downButtonContextMenu->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "arrow-down.png" ) );
  
141 142
  
  
Jan Möbius's avatar
Jan Möbius committed
143
  // General Buttons
144 145
  // Apply currently configured Mode
  connect(okButton, SIGNAL(clicked()), this, SLOT(slotChangeView()));
Jan Möbius's avatar
Jan Möbius committed
146 147
  connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
  connect(saveButton, SIGNAL(clicked()), this, SLOT(slotSaveMode()));
148 149 150 151 152 153 154 155 156 157 158 159
}


// =======================================================================================================
// View Mode button slots
// =======================================================================================================
void viewModeWidget::slotRemoveMode(){
  emit removeMode( viewModeList->currentItem()->text() );
  QListWidgetItem* item =  viewModeList->takeItem( viewModeList->currentRow() );
  delete item;
}

Jan Möbius's avatar
Jan Möbius committed
160
void viewModeWidget::slotCopyMode(){
Jan Möbius's avatar
Jan Möbius committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  // Find currently selected Mode
  // Search for current mode 
  int id = -1;
  if ( viewModeList->selectedItems().count() > 0) 
    for (int i=0; i < modes_.size(); i++)
      if (modes_[i]->name == viewModeList->currentItem()->text()){
        id = i;
        break;
      }
      
  if ( id == -1 ) {
    std::cerr << "Currently selected Mode not found?!" << std::endl; 
    return;
  }
  
  //ask for a name for the new viewmode as it is not a custom one
  bool ok;
  QString name = QInputDialog::getText(this, tr("Copy View Mode"),
                                             tr("Please enter a name for the new View Mode"), QLineEdit::Normal,
                                                "", &ok);
                     
  // Check if valid                                                
  if (!ok || name.isEmpty()) {
    QMessageBox::warning(this, tr("Copy View Mode"), tr("Please enter a Name"), QMessageBox::Ok);
    return; 
  }
  
  //check if name already exists
  for (int i=0; i < modes_.size(); i++)
    if (modes_[i]->name == name){
Jan Möbius's avatar
Jan Möbius committed
191
      QMessageBox::warning(this, tr("Copy View Mode"), tr("Cannot Copy ViewMode. \nNew Name already in use for a different mode."), QMessageBox::Ok);
Jan Möbius's avatar
Jan Möbius committed
192 193 194
      return;
    }
  
Jan Möbius's avatar
Jan Möbius committed
195
  emit saveMode(name, true, modes_[id]->visibleToolboxes, modes_[id]->visibleToolbars, modes_[id]->visibleContextMenus);      
Jan Möbius's avatar
Jan Möbius committed
196 197 198 199 200 201 202 203 204
  
  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(name);
  
  item->setForeground( QBrush(QColor(0,0,150) ) );
  
Jan Möbius's avatar
Jan Möbius committed
205
  show(name);
Jan Möbius's avatar
Jan Möbius committed
206 207 208
}

void viewModeWidget::slotAddMode(){
Jan Möbius's avatar
Jan Möbius committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  
 //ask for a name for the new viewmode as it is not a custom one
  bool ok;
  QString name = QInputDialog::getText(this, tr("Add View Mode"),
                                             tr("Please enter a name for the new View Mode"), QLineEdit::Normal,
                                                "", &ok);
                     
  // Check if valid                                                
  if (!ok || name.isEmpty()) {
    QMessageBox::warning(this, tr("Add View Mode"), tr("Please enter a Name"), QMessageBox::Ok);
    return; 
  }
  
  //check if name already exists
  for (int i=0; i < modes_.size(); i++)
    if (modes_[i]->name == name){
      QMessageBox::warning(this, tr("Add View Mode"), tr("Cannot Add ViewMode. \nNew Name already in use for a different mode."), QMessageBox::Ok);
      return;
    }
  
Jan Möbius's avatar
Jan Möbius committed
229
  emit saveMode(name, true, QStringList(), QStringList(), modes_[0]->visibleContextMenus);      
Jan Möbius's avatar
Jan Möbius committed
230 231 232 233 234 235 236 237 238 239
  
  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(name);
  
  item->setForeground( QBrush(QColor(0,0,150) ) );
  
  show(name);
Jan Möbius's avatar
Jan Möbius committed
240 241
}

242 243 244 245 246 247 248 249 250 251
// =======================================================================================================
// View Mode Context Menu
// =======================================================================================================
void viewModeWidget::slotModeContextMenu ( const QPoint & _pos ){
  
  if (viewModeList->itemAt(_pos)){
    
    QMenu menu(0);
    
    menu.addAction(tr("Remove Mode"), this, SLOT ( slotRemoveMode() ));
252 253
    menu.addAction(tr("Copy Mode"), this, SLOT ( slotCopyMode() ));      
    menu.addAction(tr("Change Icon"), this, SLOT ( slotSetIcon() ));
254 255 256 257 258
    
    //check if mode is custom e.g. that it can be removed
    for (int i=0; i < modes_.size(); i++)
      if (modes_[i]->name == viewModeList->currentItem()->text()){
        menu.actions()[0]->setEnabled(modes_[i]->custom);
259
        menu.actions()[2]->setEnabled(modes_[i]->custom);
260 261 262 263 264 265 266
        break;
      }
      
    menu.exec(viewModeList->mapToGlobal( _pos) );
  }
}

267 268 269 270 271 272 273 274 275 276 277 278 279
void viewModeWidget::slotSetIcon() {
  // Get the correct mode
  int id = -1;
  for (int i=0; i < modes_.size(); i++)
    if (modes_[i]->name == viewModeList->currentItem()->text()){
      id = i;
      break;
    }
    
  if ( id == -1) {
    std::cerr << "Unable to find Mode viewModeWidget::slotSetIcon()" << std::endl;
    return;
  }
280 281 282 283 284 285 286 287 288 289

  // Get the filename from the user
  QString fileName = QFileDialog::getOpenFileName ( this, tr("Select Image for view Mode ( best size : 150x150px )"), QString(),  tr("Images (*.png *.xpm *.jpg)") );
  
  QFile file(fileName);
  QFileInfo fileInfo(file);
  
  if ( ! file.exists() )
    return;
  
290
  
291
  file.copy(OpenFlipper::Options::configDirStr() + QDir::separator() + "Icons" + QDir::separator() + "viewMode_" + fileInfo.fileName() );
292
  
293 294 295
  modes_[id]->icon = "viewMode_" + fileInfo.fileName();

  show(modes_[id]->name);
296 297
}

298
// =======================================================================================================
Jan Möbius's avatar
Jan Möbius committed
299
// ToolBox, ToolBar and ContextMenu Lists update functions
300
// =======================================================================================================
Jan Möbius's avatar
Jan Möbius committed
301
void viewModeWidget::slotSetAllWidgets(){
302 303
  toolboxList->clear();
  toolbarList->clear();
Jan Möbius's avatar
Jan Möbius committed
304
  contextMenuList->clear();
305 306
  availableToolboxes->clear();
  availableToolbars->clear();
Jan Möbius's avatar
Jan Möbius committed
307
  availableContextMenus->clear();
308 309 310
  
  QStringList toolboxes;
  QStringList toolbars;
Jan Möbius's avatar
Jan Möbius committed
311
  QStringList contextmenus;
312 313 314 315 316 317 318 319 320
  
  //iterate over all selected modes
  for (int m=0; m < viewModeList->selectedItems().size(); m++)
    
    // find mode in modeVector modes_
    for (int i=0; i < modes_.size(); i++)
      if ( modes_[i]->name == (viewModeList->selectedItems()[m])->text() ) {
        toolboxes = modes_[i]->visibleToolboxes;
        toolbars = modes_[i]->visibleToolbars;
Jan Möbius's avatar
Jan Möbius committed
321
	contextmenus = modes_[i]->visibleContextMenus;
Jan Möbius's avatar
Jan Möbius committed
322 323
	if (contextmenus.contains("ALL_THAT_EXIST"))
	  contextmenus = modes_[0]->visibleContextMenus;
324 325
        toolboxList->addItems(toolboxes); //add corresponding widgets
        toolbarList->addItems(toolbars);
Jan Möbius's avatar
Jan Möbius committed
326
	contextMenuList->addItems(contextmenus);
327 328 329 330 331 332 333
        break;
      }
      
      
      if ( !modes_.empty() ) {
        QStringList allToolboxes = modes_[0]->visibleToolboxes;
        QStringList allToolbars  = modes_[0]->visibleToolbars;
Jan Möbius's avatar
Jan Möbius committed
334
        QStringList allContextMenus  = modes_[0]->visibleContextMenus;
335 336 337
        
        QStringList availableToolboxList;
        QStringList availableToolbarList;
Jan Möbius's avatar
Jan Möbius committed
338
        QStringList availableContextMenuList;
339 340 341 342 343 344 345 346 347 348 349
        
        for ( int i = 0; i < allToolboxes.size(); ++i ) {
          if ( ! toolboxes.contains(allToolboxes[i]) ) 
            availableToolboxList.push_back(allToolboxes[i]);
        }
        
        for ( int i = 0; i < allToolbars.size(); ++i ) {
          if ( ! toolbars.contains(allToolbars[i]) ) 
            availableToolbarList.push_back(allToolbars[i]);
        }
        
Jan Möbius's avatar
Jan Möbius committed
350 351 352 353 354
        for ( int i = 0; i < allContextMenus.size(); ++i ) {
          if ( ! contextmenus.contains(allContextMenus[i]) ) 
            availableContextMenuList.push_back(allContextMenus[i]);
        }
        
355 356
        availableToolboxes->addItems(availableToolboxList);
        availableToolbars->addItems(availableToolbarList);
Jan Möbius's avatar
Jan Möbius committed
357
        availableContextMenus->addItems(availableContextMenuList);
358 359 360 361 362
        
      } else {
        std::cerr << "Mode not found!" << std::endl;
      }
      
363 364
}

365 366 367 368 369

// =======================================================================================================
// ToolBar Context Menus Buttons
// =======================================================================================================

Jan Möbius's avatar
Jan Möbius committed
370
/// opens custom context menu in used toolbar-List
371 372 373 374 375 376 377 378 379 380 381 382 383
void viewModeWidget::slotUsedToolbarContextMenu ( const QPoint & _pos ){
  
  if (toolboxList->itemAt(_pos)){
    
    QMenu menu(0);
    
    if ( toolbarList->selectedItems().count() != 0 )
      menu.addAction(tr("Remove"), this, SLOT ( slotRightArrowToolbar() ));
    
    menu.exec(toolbarList->mapToGlobal( _pos) );
  }
}

Jan Möbius's avatar
Jan Möbius committed
384
/// opens custom context menu in available toolbar-List
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
void viewModeWidget::slotAvailableToolbarContextMenu ( const QPoint & _pos ){
  
  if (availableToolbars->itemAt(_pos)){
    
    QMenu menu(0);
    
    if ( availableToolbars->selectedItems().count() != 0 )
      menu.addAction(tr("Add"), this, SLOT ( slotLeftArrowToolbar() ));
    
    menu.exec(availableToolbars->mapToGlobal( _pos) );
  }
}

// =======================================================================================================
// ToolBox Context Menus Buttons
// =======================================================================================================

/// opens custom context menu in used toolbox-List
void viewModeWidget::slotUsedToolboxContextMenu ( const QPoint & _pos ){
  
  if (toolboxList->itemAt(_pos)){
    
    QMenu menu(0);
    
    if (toolboxList->selectedItems().count() == 1){
Jan Möbius's avatar
Jan Möbius committed
410 411
      menu.addAction(tr("Move up"), this, SLOT ( slotMoveToolboxUp() ));
      menu.addAction(tr("Move down"), this, SLOT ( slotMoveToolboxDown() ));
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
      menu.addSeparator();
    }
    
    if ( toolboxList->selectedItems().count() != 0 )
      menu.addAction(tr("Remove"), this, SLOT ( slotRightArrowToolbox() ));
    
    menu.exec(toolboxList->mapToGlobal( _pos) );
  }
}

/// opens custom context menu in available toolbox-List
void viewModeWidget::slotAvailableToolboxContextMenu ( const QPoint & _pos ){
  
  if (availableToolboxes->itemAt(_pos)){
    
    QMenu menu(0);
    
    if ( availableToolboxes->selectedItems().count() != 0 )
      menu.addAction(tr("Add"), this, SLOT ( slotLeftArrowToolbox() ));
    
    menu.exec(availableToolboxes->mapToGlobal( _pos) );
  }
}

Jan Möbius's avatar
Jan Möbius committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
// =======================================================================================================
// ContextMenu Context Menus Buttons
// =======================================================================================================

/// opens custom context menu in used contextmenu-List
void viewModeWidget::slotUsedContextMenuContextMenu ( const QPoint & _pos ){
  
  if (contextMenuList->itemAt(_pos)){
    
    QMenu menu(0);
    
    if (contextMenuList->selectedItems().count() == 1){
      menu.addAction(tr("Move up"), this, SLOT ( slotMoveContextMenuUp() ));
      menu.addAction(tr("Move down"), this, SLOT ( slotMoveContextMenuDown() ));
      menu.addSeparator();
    }
    
    if ( contextMenuList->selectedItems().count() != 0 )
      menu.addAction(tr("Remove"), this, SLOT ( slotRightArrowContextMenu() ));
    
    menu.exec(contextMenuList->mapToGlobal( _pos) );
  }
}

/// opens custom context menu in available contextmenu-List
void viewModeWidget::slotAvailableContextMenuContextMenu ( const QPoint & _pos ){
  
  if (availableContextMenus->itemAt(_pos)){
    
    QMenu menu(0);
    
    if ( availableContextMenus->selectedItems().count() != 0 )
      menu.addAction(tr("Add"), this, SLOT ( slotLeftArrowContextMenu() ));
    
    menu.exec(availableContextMenus->mapToGlobal( _pos) );
  }
}
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514

// =======================================================================================================
// ToolBar Buttons
// =======================================================================================================

void viewModeWidget::slotRightArrowToolbar() {
  QList<QListWidgetItem *> selectedItems = toolbarList->selectedItems ();
  for ( int i = 0 ; i < selectedItems.size(); ++i ) 
    availableToolbars->addItem(selectedItems[i]->text());

  qDeleteAll(selectedItems);
}

void viewModeWidget::slotLeftArrowToolbar() {
  QList<QListWidgetItem *> selectedItems = availableToolbars->selectedItems ();
  for ( int i = 0 ; i < selectedItems.size(); ++i ) 
    toolbarList->addItem(selectedItems[i]->text());
  
  qDeleteAll(selectedItems);
}

// =======================================================================================================
// ToolBox Buttons
// =======================================================================================================

void viewModeWidget::slotRightArrowToolbox() {
  QList<QListWidgetItem *> selectedItems = toolboxList->selectedItems ();
  for ( int i = 0 ; i < selectedItems.size(); ++i ) 
    availableToolboxes->addItem(selectedItems[i]->text());
  
  qDeleteAll(selectedItems);
}

void viewModeWidget::slotLeftArrowToolbox() {
  QList<QListWidgetItem *> selectedItems = availableToolboxes->selectedItems ();
  for ( int i = 0 ; i < selectedItems.size(); ++i ) 
    toolboxList->addItem(selectedItems[i]->text());
  
  qDeleteAll(selectedItems);
}


515
/// Move Widget up in the list
Jan Möbius's avatar
Jan Möbius committed
516
void viewModeWidget::slotMoveToolboxUp(){
517 518
  if (toolboxList->selectedItems().count() == 1){
    if (toolboxList->currentRow() == 0) return;
519
    //extract a list of all items
520 521
    QString item = toolboxList->currentItem()->text();
    int oldRow = toolboxList->currentRow();
522
    QStringList widgets;
523 524
    for (int i=0; i < toolboxList->count(); i++)
      widgets << toolboxList->item(i)->text();
525
    
526 527 528 529 530 531 532 533 534
    //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];
    }
535 536 537
    // set new list as elements for the toolboxList
    toolboxList->clear();
    toolboxList->addItems(widgets);
538
    //highlight active item
539
    toolboxList->setCurrentRow(oldRow-1);
540 541 542
  }
}

Jan Möbius's avatar
Jan Möbius committed
543 544
/// Move widget down in the list
void viewModeWidget::slotMoveToolboxDown(){
545 546
  if (toolboxList->selectedItems().count() == 1){
    if (toolboxList->currentRow() == toolboxList->count()-1) return;
547
    //extract a list of all items
548 549
    QString item = toolboxList->currentItem()->text();
    int oldRow = toolboxList->currentRow();
550
    QStringList widgets;
551 552
    for (int i=0; i < toolboxList->count(); i++)
      widgets << toolboxList->item(i)->text();
553
    
554 555 556 557 558 559 560 561 562
    //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];
    }
563 564 565
    // set new list as elements for the toolboxList
    toolboxList->clear();
    toolboxList->addItems(widgets);
566
    //highlight active item
567
    toolboxList->setCurrentRow(oldRow+1);
568 569 570
  }
}

Jan Möbius's avatar
Jan Möbius committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
// =======================================================================================================
// ContextMenu Buttons
// =======================================================================================================

void viewModeWidget::slotRightArrowContextMenu() {
  QList<QListWidgetItem *> selectedItems = contextMenuList->selectedItems ();
  for ( int i = 0 ; i < selectedItems.size(); ++i ) 
    availableContextMenus->addItem(selectedItems[i]->text());
  
  qDeleteAll(selectedItems);
}

void viewModeWidget::slotLeftArrowContextMenu() {
  QList<QListWidgetItem *> selectedItems = availableContextMenus->selectedItems ();
  for ( int i = 0 ; i < selectedItems.size(); ++i ) 
    contextMenuList->addItem(selectedItems[i]->text());
  
  qDeleteAll(selectedItems);
}


/// Move Widget up in the list
void viewModeWidget::slotMoveContextMenuUp(){
  if (contextMenuList->selectedItems().count() == 1){
    if (contextMenuList->currentRow() == 0) return;
    //extract a list of all items
    QString item = contextMenuList->currentItem()->text();
    int oldRow = contextMenuList->currentRow();
    QStringList widgets;
    for (int i=0; i < contextMenuList->count(); i++)
      widgets << contextMenuList->item(i)->text();
    
    //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];
    }
    // set new list as elements for the toolboxList
    contextMenuList->clear();
    contextMenuList->addItems(widgets);
    //highlight active item
    contextMenuList->setCurrentRow(oldRow-1);
  }
}

/// Move widget down in the list
void viewModeWidget::slotMoveContextMenuDown(){
  if (contextMenuList->selectedItems().count() == 1){
    if (contextMenuList->currentRow() == contextMenuList->count()-1) return;
    //extract a list of all items
    QString item = contextMenuList->currentItem()->text();
    int oldRow = contextMenuList->currentRow();
    QStringList widgets;
    for (int i=0; i < contextMenuList->count(); i++)
      widgets << contextMenuList->item(i)->text();
    
    //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];
    }
    // set new list as elements for the toolboxList
    contextMenuList->clear();
    contextMenuList->addItems(widgets);
    //highlight active item
    contextMenuList->setCurrentRow(oldRow+1);
  }
}

648

649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665

// =======================================================================================================
// External communication
// =======================================================================================================

/// Slot for changing View and closing widget
void viewModeWidget::slotChangeView(){
  //get toolboxes
  QStringList toolboxes;
  for (int i=0; i < toolboxList->count(); i++)
    toolboxes << toolboxList->item(i)->text();
  
  //get toolbars
  QStringList toolbars;
  for (int i=0; i < toolbarList->count(); i++)
    toolbars << toolbarList->item(i)->text();
  
Jan Möbius's avatar
Jan Möbius committed
666 667 668 669 670
  //get context menus
  QStringList contextmenus;
  for (int i=0; i < contextMenuList->count(); i++)
    contextmenus << contextMenuList->item(i)->text();
  
671 672 673 674 675
  //get mode
  QString mode = "";
  if (viewModeList->selectedItems().size() > 0)
    mode = viewModeList->selectedItems()[0]->text();
  
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
  
  // Check current configuration if it is a changed view mode
  
  // Search for current mode 
  int id = -1;
  if ( viewModeList->selectedItems().count() > 0) 
    for (int i=0; i < modes_.size(); i++)
      if (modes_[i]->name == viewModeList->currentItem()->text()){
        id = i;
        break;
      }
      
  if ( id == -1 ) {
    std::cerr << "Currently selected Mode not found?!" << std::endl; 
    return;
  }
  
  bool matching = true;
  // Check if toolbox list matches:
  if ( modes_[id]->visibleToolboxes.size() == toolboxes.size() ) {
    for ( int i = 0 ; i < modes_[id]->visibleToolboxes.size(); ++i  ) 
      if ( modes_[id]->visibleToolboxes[i] != toolboxes[i] )
        matching = false;
  } else {
      matching = false;
  }
  
  // Check if toolbar list matches:
  if ( modes_[id]->visibleToolbars.size() == toolbars.size() ) {
    for ( int i = 0 ; i < modes_[id]->visibleToolbars.size(); ++i  ) 
      if ( modes_[id]->visibleToolbars[i] != toolbars[i] )
        matching = false;
  } else {
    matching = false;
  }
  
Jan Möbius's avatar
Jan Möbius committed
712 713 714 715 716 717 718 719 720
  // Check if context menu list matches:
  if ( modes_[id]->visibleContextMenus.size() == contextmenus.size() ) {
    for ( int i = 0 ; i < modes_[id]->visibleContextMenus.size(); ++i  ) 
      if ( modes_[id]->visibleContextMenus[i] != contextmenus[i] )
        matching = false;
  } else {
    matching = false;
  }
  
721 722 723 724 725 726 727 728 729 730 731
  if ( !matching ) {
    int ret = QMessageBox::warning(this, 
                                   tr("Mode has been changed!"),
                                   tr("You changed the view mode configuration. Do you want to save it?"),
                                   QMessageBox::Yes|QMessageBox::No,
                                   QMessageBox::No);
    if (ret == QMessageBox::Yes) 
      slotSaveMode();
    
  }
  
Jan Möbius's avatar
Jan Möbius committed
732
  emit changeView(mode,toolboxes,toolbars,contextmenus);
733
  close();
734 735
}

736 737 738 739 740
/// Slot for saving current List of Widgets as custom mode
void viewModeWidget::slotSaveMode(){
  // Search for current mode vector
  int id = -1;
  if ( viewModeList->selectedItems().count() > 0) 
741
    for (int i=0; i < modes_.size(); i++)
742
      if (modes_[i]->name == viewModeList->currentItem()->text()){
743
        id = i;
744 745
        break;
      }
746 747
  
  if ( id == -1 ) {
748
    std::cerr << "Mode Not found in slotSaveMode" << std::endl;
749
    return;
750
  }
751 752 753 754 755 756 757 758 759 760
  
  // Get Toolboxes
  QStringList toolboxes;
  for (int i=0; i < toolboxList->count(); i++)
    toolboxes << toolboxList->item(i)->text();
  
  // Get Toolbars
  QStringList toolbars;
  for (int i=0; i < toolbarList->count(); i++)
    toolbars << toolbarList->item(i)->text();
Jan Möbius's avatar
Jan Möbius committed
761 762 763 764 765
  
  // Get Context Menus
  QStringList contextmenus;
  for (int i=0; i < contextMenuList->count(); i++)
    contextmenus << contextMenuList->item(i)->text();
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
   
  bool  createNewMode = false;

  QString message = tr("You cannot change predefined modes.\n"
                       "Please enter a new Name for the mode.");
  
  // Check if we want to create a new node.
  if ( ! modes_[id]->custom ) {
    createNewMode = true;
    
  } else {
    int ret = QMessageBox::warning(this, 
                                   tr("View Mode exists"),
                                   tr("View Mode already exists. Do you want to overwrite it?"),
                                   QMessageBox::Yes|QMessageBox::No,
                                   QMessageBox::No);
    if (ret == QMessageBox::No) {
      message = tr("New name for view mode:");
      createNewMode = true;
    }
  }
  
  
  if ( createNewMode ) {
    
    //ask for a name for the new viewmode as it is not a custom one
    bool ok;
    QString name = QInputDialog::getText(this, tr("Save view Mode"),
                                               message, QLineEdit::Normal,
                                                  "", &ok);
                                                    
    //Remove Spaces from name
    if (!ok || name.isEmpty()) {
      std::cerr << "Illegal or no name given!" << std::endl;
      return; 
    }
    
    //check if name already exists
    for (int i=0; i < modes_.size(); i++)
      if (modes_[i]->name == name){
Jan Möbius's avatar
Jan Möbius committed
806
        QMessageBox::warning(this, tr("Save View Mode"), tr("Cannot Save ViewMode.\nName already taken by a different mode."), QMessageBox::Ok);
807 808 809
        return;
      }
      
Jan Möbius's avatar
Jan Möbius committed
810
    emit saveMode(name, true, toolboxes, toolbars, contextmenus);
811 812
    show(name);      
  } else {
Jan Möbius's avatar
Jan Möbius committed
813
    emit saveMode(modes_[id]->name, true, toolboxes, toolbars, contextmenus);
814 815 816
    show(modes_[id]->name);    
  }
  
Jan Möbius's avatar
Jan Möbius committed
817
  slotModeChanged(QString());
818 819
}  

820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840


















/// overloaded show function
void viewModeWidget::show(QString _lastMode){
  QDialog::show();
841
  
842 843
  //fill viewModeList
  viewModeList->clear();
844
  for (int i=0; i < modes_.size(); i++){
845 846 847
    QListWidgetItem *item = new QListWidgetItem(viewModeList);
    item->setTextAlignment(Qt::AlignHCenter);
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
848 849 850 851 852 853 854 855 856 857 858 859 860 861
    
    QFile iconFile( OpenFlipper::Options::iconDirStr() + QDir::separator () + modes_[i]->icon  );
    
    if ( iconFile.exists() )
      item->setIcon( QIcon(iconFile.fileName()) );
    else {
      iconFile.setFileName( OpenFlipper::Options::configDirStr() + QDir::separator() + "Icons" + QDir::separator() + modes_[i]->icon );
      if ( iconFile.exists() )
        item->setIcon( QIcon(iconFile.fileName()) );
      else {
        item->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + QDir::separator () + "Unknown.png")  );
        std::cerr << "Unable to find icon file! " << iconFile.fileName().toStdString() <<  std::endl;
      }
    }
Jan Möbius's avatar
Jan Möbius committed
862
    
863
    item->setText(modes_[i]->name);
864
    
865
    if (modes_[i]->custom)
866
      viewModeList->item(i)->setForeground( QBrush(QColor(0,0,150) ) );
867
    else
868
      viewModeList->item(i)->setForeground( QBrush(QColor(0,0,0) ) );
869
  }
870
  
871
  //select given mode
872
  viewModeList->setCurrentRow(0);
873
  
874 875 876
  for (int i=0; i < viewModeList->count(); i++)
    if (viewModeList->item(i)->text() == _lastMode)
      viewModeList->setCurrentRow(i);
877 878 879 880
    
    removeButton->setEnabled(false);
}

881

882 883 884
/// Slot for updating removeButton when new mode is selected
void viewModeWidget::slotModeClicked(QModelIndex /*_id*/){
  slotModeChanged(QString());
885 886
}

887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
/// Slot for updating removeButton when new mode is selected
void viewModeWidget::slotModeChanged(QString /*_mode*/){
  //check if mode is custom e.g. that it can be removed
  if (viewModeList->selectedItems().count() > 0){
    for (int i=0; i < modes_.size(); i++)
      if (modes_[i]->name == viewModeList->currentItem()->text()){
        removeButton->setEnabled(modes_[i]->custom);
        break;
      }
  }
}