BackupPlugin.cc 13.9 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
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/>.                                       *
*                                                                            *
Jan Möbius's avatar
Jan Möbius committed
33
34
35
\*===========================================================================*/

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

#include <QtGui>
#include "BackupPlugin.hh"
#include <iostream>
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <OpenFlipper/common/GlobalOptions.hh>

Dirk Wilden's avatar
Dirk Wilden committed
49
#include "GroupBackup.hh"
Jan Möbius's avatar
 
Jan Möbius committed
50

Dirk Wilden's avatar
Dirk Wilden committed
51
//-----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
52
53
54
55
56
57
58

void BackupPlugin::pluginsInitialized() {

  // Create Backup menu
  emit getMenubarMenu(tr("&Backup"), backupMenu_, true );

  if ( OpenFlipper::Options::gui() ) {
Dirk Wilden's avatar
Dirk Wilden committed
59
    //construct the menu
Jan Möbius's avatar
 
Jan Möbius committed
60
61
62
    backupsEnabledAction_ = new QAction("Backups Enabled",0);
    backupsEnabledAction_->setCheckable(true);
    connect (backupsEnabledAction_, SIGNAL(triggered(bool)), this, SLOT(slotEnableDisableBackups()) );
Dirk Wilden's avatar
Dirk Wilden committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    
    undoMenuAction_ = new QAction(tr("&Undo"), this);
    undoMenuAction_->setEnabled(false);
    undoMenuAction_->setStatusTip(tr("Undo the last action."));
    undoMenuAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"edit-undo.png") );
    connect(undoMenuAction_, SIGNAL(triggered()), this, SLOT(slotUndo()) );
    
    redoMenuAction_ = new QAction(tr("&Redo"), this);
    redoMenuAction_->setEnabled(false);
    redoMenuAction_->setStatusTip(tr("Redo the last action"));
    redoMenuAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"edit-redo.png") );
    connect(redoMenuAction_, SIGNAL(triggered()), this, SLOT(slotRedo()) );

    backupMenu_->addAction(undoMenuAction_);
    backupMenu_->addAction(redoMenuAction_);
    backupMenu_->addSeparator();
    backupMenu_->addAction(backupsEnabledAction_);
Jan Möbius's avatar
 
Jan Möbius committed
80
81
82
83
84

    // Add a backup Toolbar
    QToolBar* toolbar = new QToolBar("Backup Toolbar");
    
    //Undo
Dirk Wilden's avatar
Dirk Wilden committed
85
86
87
88
89
90
    undoToolAction_ = new QAction(tr("&Undo"), this);
    undoToolAction_->setEnabled(false);
    undoToolAction_->setStatusTip(tr("Undo the last action."));
    undoToolAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"edit-undo.png") );
    connect(undoToolAction_, SIGNAL(triggered()), this, SLOT(slotUndo()) );
    toolbar->addAction(undoToolAction_);
Jan Möbius's avatar
 
Jan Möbius committed
91
92
    
    //Redo
Dirk Wilden's avatar
Dirk Wilden committed
93
94
95
96
97
98
    redoToolAction_ = new QAction(tr("&Redo"), this);
    redoToolAction_->setEnabled(false);
    redoToolAction_->setStatusTip(tr("Redo the last action"));
    redoToolAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"edit-redo.png") );
    connect(redoToolAction_, SIGNAL(triggered()), this, SLOT(slotRedo()) );
    toolbar->addAction(redoToolAction_);
Jan Möbius's avatar
 
Jan Möbius committed
99
100
101
102
103
104
  
    emit addToolbar( toolbar );
    
    //the release event does not contain the modifier
    emit registerKey(Qt::Key_Z,  Qt::ControlModifier, tr("Undo Action"));
    emit registerKey(Qt::Key_Z, (Qt::ControlModifier | Qt::ShiftModifier),   tr("Redo Action"));
Dirk Wilden's avatar
Dirk Wilden committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    
    //add actions for the context menu
    undoContextAction_ = new QAction(tr("&Undo"), this);
    undoContextAction_->setEnabled(false);
    undoContextAction_->setStatusTip(tr("Undo the last action."));
    undoContextAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"edit-undo.png") );
    connect(undoContextAction_, SIGNAL(triggered()), this, SLOT(slotObjectUndo()) );
    emit addContextMenuItem(undoContextAction_, DATA_ALL, CONTEXTOBJECTMENU);

    redoContextAction_ = new QAction(tr("&Redo"), this);
    redoContextAction_->setEnabled(false);
    redoContextAction_->setStatusTip(tr("Redo the last action"));
    redoContextAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"edit-redo.png") );
    connect(redoContextAction_, SIGNAL(triggered()), this, SLOT(slotObjectRedo()) );
    emit addContextMenuItem(redoContextAction_, DATA_ALL, CONTEXTOBJECTMENU);
Jan Möbius's avatar
 
Jan Möbius committed
120
121
122
  }
}

Dirk Wilden's avatar
Dirk Wilden committed
123
124
//-----------------------------------------------------------------------------

Jan Möbius's avatar
 
Jan Möbius committed
125
void BackupPlugin::slotAllCleared(){
Dirk Wilden's avatar
Dirk Wilden committed
126
127
  globalBackup_.clear();
}
Jan Möbius's avatar
 
Jan Möbius committed
128

Dirk Wilden's avatar
Dirk Wilden committed
129
130
131
//-----------------------------------------------------------------------------

void BackupPlugin::updateButtons() {
Jan Möbius's avatar
 
Jan Möbius committed
132
  
Dirk Wilden's avatar
Dirk Wilden committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  if ( globalBackup_.undoAvailable() ){
    undoMenuAction_->setText( tr("Undo '%1'").arg( globalBackup_.undoName() ) );
    undoMenuAction_->setEnabled(true);
    undoToolAction_->setText( tr("Undo '%1'").arg( globalBackup_.undoName() ) );
    undoToolAction_->setEnabled(true);
  } else {
    undoMenuAction_->setText( tr("Undo") );
    undoMenuAction_->setEnabled(false);
    undoToolAction_->setText( tr("Undo") );
    undoToolAction_->setEnabled(false);
  }

  if ( globalBackup_.redoAvailable() ){
    redoMenuAction_->setText( tr("Redo '%1'").arg( globalBackup_.redoName() ) );
    redoMenuAction_->setEnabled(true);
    redoToolAction_->setText( tr("Redo '%1'").arg( globalBackup_.redoName() ) );
    redoToolAction_->setEnabled(true);
  } else {
    redoMenuAction_->setText( tr("Redo") );
    redoMenuAction_->setEnabled(false);
    redoToolAction_->setText( tr("Redo") );
    redoToolAction_->setEnabled(false);
  }
Jan Möbius's avatar
 
Jan Möbius committed
156

Dirk Wilden's avatar
Dirk Wilden committed
157
  backupsEnabledAction_->setChecked( OpenFlipper::Options::backupEnabled() );
Jan Möbius's avatar
 
Jan Möbius committed
158
159
}

Dirk Wilden's avatar
Dirk Wilden committed
160
161
//-----------------------------------------------------------------------------

Jan Möbius's avatar
 
Jan Möbius committed
162
163
164
165
void BackupPlugin::slotEnableDisableBackups() {
  OpenFlipper::Options::enableBackup(backupsEnabledAction_->isChecked());
}

Dirk Wilden's avatar
Dirk Wilden committed
166
167
//-----------------------------------------------------------------------------

Jan Möbius's avatar
 
Jan Möbius committed
168
169
170
171
172
void BackupPlugin::slotKeyEvent( QKeyEvent* _event ){
   switch (_event->key())
   {
      case Qt::Key_Z :
         if ( _event->modifiers() == Qt::ControlModifier )
Dirk Wilden's avatar
Dirk Wilden committed
173
           emit undo();
Jan Möbius's avatar
 
Jan Möbius committed
174
         else if ( _event->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier) )
Dirk Wilden's avatar
Dirk Wilden committed
175
           emit redo();
Jan Möbius's avatar
 
Jan Möbius committed
176
177
178
179
180
181
         break;
      default:
         break;
  }
}

Dirk Wilden's avatar
Dirk Wilden committed
182
183
//-----------------------------------------------------------------------------

Jan Möbius's avatar
 
Jan Möbius committed
184
185
186
void BackupPlugin::objectDeleted(int _objectid) {
}

Dirk Wilden's avatar
Dirk Wilden committed
187
//-----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
188

Dirk Wilden's avatar
Dirk Wilden committed
189
void BackupPlugin::slotUpdateContextMenu( int _objectId ){
Jan Möbius's avatar
 
Jan Möbius committed
190

Dirk Wilden's avatar
Dirk Wilden committed
191
192
193
194
195
  //disable everything
  undoContextAction_->setText( tr("Undo") );
  undoContextAction_->setEnabled(false);
  redoContextAction_->setText( tr("Redo") );
  redoContextAction_->setEnabled(false);
Jan Möbius's avatar
 
Jan Möbius committed
196

Dirk Wilden's avatar
Dirk Wilden committed
197
198
199
  //get backup data
  BaseObjectData* object = 0;
  PluginFunctions::getObject(_objectId, object);
Jan Möbius's avatar
 
Jan Möbius committed
200

Dirk Wilden's avatar
Dirk Wilden committed
201
  if ( object != 0 ){
Jan Möbius's avatar
 
Jan Möbius committed
202

Dirk Wilden's avatar
Dirk Wilden committed
203
204
    //get backup object data
    BackupData* backupData = 0;
Jan Möbius's avatar
 
Jan Möbius committed
205

Dirk Wilden's avatar
Dirk Wilden committed
206
207
    if ( object->hasObjectData( OBJECT_BACKUPS ) ){
      backupData = dynamic_cast< BackupData* >(object->objectData(OBJECT_BACKUPS));
Jan Möbius's avatar
 
Jan Möbius committed
208

Dirk Wilden's avatar
Dirk Wilden committed
209
210
211
212
213
      if ( backupData->undoAvailable() ){
        undoContextAction_->setData(_objectId);
        undoContextAction_->setText( tr("Undo '%1'").arg( backupData->undoName() ) );
        undoContextAction_->setEnabled( !backupData->undoBlocked() );
      }
Jan Möbius's avatar
 
Jan Möbius committed
214

Dirk Wilden's avatar
Dirk Wilden committed
215
216
217
218
      if ( backupData->redoAvailable() ){
        redoContextAction_->setData(_objectId);
        redoContextAction_->setText( tr("Redo '%1'").arg( backupData->redoName() ) );
        redoContextAction_->setEnabled( !backupData->redoBlocked() );
Jan Möbius's avatar
 
Jan Möbius committed
219
220
221
      }
    }
  }
Dirk Wilden's avatar
Dirk Wilden committed
222
223
224
225

  //TODO do it correct
  emit addContextMenuItem(redoContextAction_, DATA_ALL, CONTEXTOBJECTMENU);
  emit addContextMenuItem(undoContextAction_, DATA_ALL, CONTEXTOBJECTMENU);
Jan Möbius's avatar
 
Jan Möbius committed
226
227
}

Dirk Wilden's avatar
Dirk Wilden committed
228
//-----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
229

Dirk Wilden's avatar
Dirk Wilden committed
230
void BackupPlugin::slotObjectUndo(){
Jan Möbius's avatar
 
Jan Möbius committed
231
  
Dirk Wilden's avatar
Dirk Wilden committed
232
233
  int id = undoContextAction_->data().toInt();
  emit undo(id);
Jan Möbius's avatar
 
Jan Möbius committed
234
235
}

Dirk Wilden's avatar
Dirk Wilden committed
236
//-----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
237

Dirk Wilden's avatar
Dirk Wilden committed
238
void BackupPlugin::slotObjectRedo(){
Jan Möbius's avatar
 
Jan Möbius committed
239
  
Dirk Wilden's avatar
Dirk Wilden committed
240
241
242
  int id = undoContextAction_->data().toInt();
  emit redo(id);
}
Jan Möbius's avatar
 
Jan Möbius committed
243

Dirk Wilden's avatar
Dirk Wilden committed
244
//-----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
245

Dirk Wilden's avatar
Dirk Wilden committed
246
247
248
249
void BackupPlugin::slotCreateBackup( int _objectid, QString _name, UpdateType _type){
  
  if ( !OpenFlipper::Options::backupEnabled() )
    return;
Jan Möbius's avatar
 
Jan Möbius committed
250

Dirk Wilden's avatar
Dirk Wilden committed
251
  BaseObjectData* object;
Jan Möbius's avatar
 
Jan Möbius committed
252

Dirk Wilden's avatar
Dirk Wilden committed
253
254
255
256
  if ( !PluginFunctions::getObject(_objectid,object) ){
    emit log(LOGWARN,"Unable to find backup object with id " + QString::number(_objectid));
    return;
  }
Jan Möbius's avatar
 
Jan Möbius committed
257

Dirk Wilden's avatar
Dirk Wilden committed
258
259
  //tell TypePlugin to Backup
  emit generateBackup( _objectid, _name, _type );
Jan Möbius's avatar
 
Jan Möbius committed
260

Dirk Wilden's avatar
Dirk Wilden committed
261
262
263
  //add global backup
  IdList groupIds;
  groupIds.push_back( _objectid );
Jan Möbius's avatar
 
Jan Möbius committed
264

Dirk Wilden's avatar
Dirk Wilden committed
265
  bool skipBackup = false;
Jan Möbius's avatar
 
Jan Möbius committed
266

Dirk Wilden's avatar
Dirk Wilden committed
267
268
269
  if ( globalBackup_.undoAvailable() ){
    //skip 'Original Object' Backups in global view
    BackupData* backupData = dynamic_cast< BackupData* >(object->objectData(OBJECT_BACKUPS));
Jan Möbius's avatar
 
Jan Möbius committed
270

Dirk Wilden's avatar
Dirk Wilden committed
271
272
273
    if ( backupData != 0 )
      skipBackup = !backupData->undoAvailable();
  }
Jan Möbius's avatar
 
Jan Möbius committed
274

Dirk Wilden's avatar
Dirk Wilden committed
275
276
277
278
  if ( !skipBackup ){
    GroupBackup* backup = new GroupBackup(groupIds, _name);
    globalBackup_.storeBackup( backup );
  }
Jan Möbius's avatar
 
Jan Möbius committed
279

Dirk Wilden's avatar
Dirk Wilden committed
280
281
282
  updateButtons();
  emit log(LOGOUT,"Created backup for " + QString::number(_objectid)+ " , Name : '" + _name + "'" );
}
Jan Möbius's avatar
 
Jan Möbius committed
283

Dirk Wilden's avatar
Dirk Wilden committed
284
//-----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
285

Dirk Wilden's avatar
Dirk Wilden committed
286
void BackupPlugin::slotCreateBackup( IdList _objectids , QString _name, std::vector<UpdateType> _types){
Jan Möbius's avatar
 
Jan Möbius committed
287

Dirk Wilden's avatar
Dirk Wilden committed
288
289
  if ( !OpenFlipper::Options::backupEnabled() )
    return;
Jan Möbius's avatar
 
Jan Möbius committed
290

Dirk Wilden's avatar
Dirk Wilden committed
291
  IdList groupIds;
Jan Möbius's avatar
 
Jan Möbius committed
292
  
Dirk Wilden's avatar
Dirk Wilden committed
293
294
  if ( _objectids.size() != _types.size() ){
    emit log(LOGWARN,"Unable to create backup sizes of ids and updateTypes do not match!");
Jan Möbius's avatar
 
Jan Möbius committed
295
296
297
    return;
  }

Dirk Wilden's avatar
Dirk Wilden committed
298
299
  //generate backups on all objects
  for (unsigned int i=0; i < _objectids.size(); ++i){
Jan Möbius's avatar
 
Jan Möbius committed
300

Dirk Wilden's avatar
Dirk Wilden committed
301
    BaseObjectData* object;
Jan Möbius's avatar
 
Jan Möbius committed
302

Dirk Wilden's avatar
Dirk Wilden committed
303
304
305
306
    if ( !PluginFunctions::getObject(_objectids[i],object) ){
      emit log(LOGWARN,"Unable to find backup object with id " + QString::number(_objectids[i]));
      continue;
    }
Jan Möbius's avatar
 
Jan Möbius committed
307

Dirk Wilden's avatar
Dirk Wilden committed
308
309
310
311
    //tell TypePlugin to generate a backup
    emit generateBackup( _objectids[i], _name, _types[i] );
    groupIds.push_back(  _objectids[i] );
  }
Jan Möbius's avatar
 
Jan Möbius committed
312

Dirk Wilden's avatar
Dirk Wilden committed
313
314
315
316
317
  //add global backup
  if ( groupIds.size() > 0 ){
    
    GroupBackup* backup = new GroupBackup(groupIds, _name);
    globalBackup_.storeBackup( backup );
Jan Möbius's avatar
 
Jan Möbius committed
318

Dirk Wilden's avatar
Dirk Wilden committed
319
320
    updateButtons();
    emit log(LOGOUT,"Created grouped backup, Name : '" + _name + "'" );
Jan Möbius's avatar
 
Jan Möbius committed
321
322
323
  }
}

Dirk Wilden's avatar
Dirk Wilden committed
324
//-----------------------------------------------------------------------------
325

Dirk Wilden's avatar
Dirk Wilden committed
326
void BackupPlugin::slotUndo(int _objectid){
327

Dirk Wilden's avatar
Dirk Wilden committed
328
329
  globalBackup_.undo(_objectid);
  emit updatedObject(_objectid, UPDATE_ALL);
330

Dirk Wilden's avatar
Dirk Wilden committed
331
  updateButtons();
Jan Möbius's avatar
 
Jan Möbius committed
332
333
}

Dirk Wilden's avatar
Dirk Wilden committed
334
//-----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
335

Dirk Wilden's avatar
Dirk Wilden committed
336
337
void BackupPlugin::slotUndo(){
  globalBackup_.undo();
Jan Möbius's avatar
 
Jan Möbius committed
338

Dirk Wilden's avatar
Dirk Wilden committed
339
  GroupBackup* group = dynamic_cast< GroupBackup* >( globalBackup_.currentState() );
Jan Möbius's avatar
 
Jan Möbius committed
340
    
Dirk Wilden's avatar
Dirk Wilden committed
341
342
343
  if ( group != 0)
    for (unsigned int i=0; i < group->objectIDs().size(); i++)
      emit updatedObject(group->objectIDs()[i], UPDATE_ALL);
Jan Möbius's avatar
 
Jan Möbius committed
344

Dirk Wilden's avatar
Dirk Wilden committed
345
346
  updateButtons();
}
Jan Möbius's avatar
 
Jan Möbius committed
347

Dirk Wilden's avatar
Dirk Wilden committed
348
//-----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
349

Dirk Wilden's avatar
Dirk Wilden committed
350
void BackupPlugin::slotRedo(int _objectid){
Jan Möbius's avatar
 
Jan Möbius committed
351

Dirk Wilden's avatar
Dirk Wilden committed
352
353
  globalBackup_.redo(_objectid);
  emit updatedObject(_objectid, UPDATE_ALL);
Jan Möbius's avatar
 
Jan Möbius committed
354

Dirk Wilden's avatar
Dirk Wilden committed
355
  updateButtons();
Jan Möbius's avatar
 
Jan Möbius committed
356
357
}

Dirk Wilden's avatar
Dirk Wilden committed
358
//-----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
359

Dirk Wilden's avatar
Dirk Wilden committed
360
361
void BackupPlugin::slotRedo(){
  globalBackup_.redo();
Jan Möbius's avatar
 
Jan Möbius committed
362

Dirk Wilden's avatar
Dirk Wilden committed
363
  GroupBackup* group = dynamic_cast< GroupBackup* >( globalBackup_.currentState() );
Jan Möbius's avatar
 
Jan Möbius committed
364

Dirk Wilden's avatar
Dirk Wilden committed
365
366
367
  if ( group != 0)
    for (unsigned int i=0; i < group->objectIDs().size(); i++)
      emit updatedObject(group->objectIDs()[i], UPDATE_ALL);
Jan Möbius's avatar
 
Jan Möbius committed
368

Dirk Wilden's avatar
Dirk Wilden committed
369
  updateButtons();
Jan Möbius's avatar
 
Jan Möbius committed
370
371
}

Dirk Wilden's avatar
Dirk Wilden committed
372
//-----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
373
374
375

Q_EXPORT_PLUGIN2( backupplugin , BackupPlugin );