44#include "BackupPlugin.hh"
49#include "GroupBackup.hh"
55BackupPlugin::BackupPlugin():
58backupsEnabledAction_(0),
72void BackupPlugin::initializePlugin()
77 OpenFlipper::Options::enableBackup(
OpenFlipperSettings().value(
"BackupPlugin/EnableBackups",
true).toBool());
82void BackupPlugin::pluginsInitialized() {
85 emit getMenubarMenu(tr(
"&Backup"),
backupMenu_,
true );
87 if ( OpenFlipper::Options::gui() ) {
89 backupsEnabledAction_ =
new QAction(
"Backups Enabled",0);
90 backupsEnabledAction_->setCheckable(
true);
91 backupsEnabledAction_->setChecked(OpenFlipper::Options::backupEnabled());
94 undoMenuAction_ =
new QAction(tr(
"&Undo"),
this);
95 undoMenuAction_->setEnabled(
false);
96 undoMenuAction_->setStatusTip(tr(
"Undo the last action."));
97 undoMenuAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-undo.png") );
98 connect(undoMenuAction_, SIGNAL(triggered()),
this, SIGNAL( undo() ) );
100 redoMenuAction_ =
new QAction(tr(
"&Redo"),
this);
101 redoMenuAction_->setEnabled(
false);
102 redoMenuAction_->setStatusTip(tr(
"Redo the last action"));
103 redoMenuAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-redo.png") );
104 connect(redoMenuAction_, SIGNAL(triggered()),
this, SIGNAL( redo() ) );
112 QToolBar* toolbar =
new QToolBar(
"Backup Toolbar");
115 undoToolAction_ =
new QAction(tr(
"&Undo"),
this);
116 undoToolAction_->setEnabled(
false);
117 undoToolAction_->setStatusTip(tr(
"Undo the last action."));
118 undoToolAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-undo.png") );
119 connect(undoToolAction_, SIGNAL(triggered()),
this, SIGNAL( undo() ) );
120 toolbar->addAction(undoToolAction_);
123 redoToolAction_ =
new QAction(tr(
"&Redo"),
this);
124 redoToolAction_->setEnabled(
false);
125 redoToolAction_->setStatusTip(tr(
"Redo the last action"));
126 redoToolAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-redo.png") );
127 connect(redoToolAction_, SIGNAL(triggered()),
this, SIGNAL( redo() ) );
128 toolbar->addAction(redoToolAction_);
130 emit addToolbar( toolbar );
133 emit registerKey(Qt::Key_Z, Qt::ControlModifier, tr(
"Undo Action"));
134 emit registerKey(Qt::Key_Z, (Qt::ControlModifier | Qt::ShiftModifier), tr(
"Redo Action"));
137 undoContextAction_ =
new QAction(tr(
"&Undo"),
this);
138 undoContextAction_->setEnabled(
false);
139 undoContextAction_->setStatusTip(tr(
"Undo the last action."));
140 undoContextAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-undo.png") );
141 connect(undoContextAction_, SIGNAL(triggered()),
this, SLOT(slotObjectUndo()) );
144 redoContextAction_ =
new QAction(tr(
"&Redo"),
this);
145 redoContextAction_->setEnabled(
false);
146 redoContextAction_->setStatusTip(tr(
"Redo the last action"));
147 redoContextAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-redo.png") );
148 connect(redoContextAction_, SIGNAL(triggered()),
this, SLOT(slotObjectRedo()) );
152 if (maxBackupSpinBox_)
153 maxBackupSpinBox_->setValue(globalBackup_.
maxBackups());
158 emit setSlotDescription(
"createBackup(int,QString)", tr(
"Creates a backup which can be restored via undo."),
159 QString(
"objectId,name").split(
","), QString(
"Id of the object,name of the backup").split(
","));
160 emit setSlotDescription(
"createBackup(int,QString,UpdateType)", tr(
"Creates a backup which can be restored via undo."),
161 QString(
"objectId,name,type").split(
","), QString(
"Id of the object,name of the backup,updatetypes which are changed").split(
","));
166void BackupPlugin::slotAllCleared(){
167 globalBackup_.
clear();
173void BackupPlugin::updateButtons() {
176 undoMenuAction_->setText( tr(
"Undo '%1'").arg( globalBackup_.
undoName() ) );
177 undoMenuAction_->setEnabled(
true);
178 undoToolAction_->setText( tr(
"Undo '%1'").arg( globalBackup_.
undoName() ) );
179 undoToolAction_->setEnabled(
true);
181 undoMenuAction_->setText( tr(
"Undo") );
182 undoMenuAction_->setEnabled(
false);
183 undoToolAction_->setText( tr(
"Undo") );
184 undoToolAction_->setEnabled(
false);
188 redoMenuAction_->setText( tr(
"Redo '%1'").arg( globalBackup_.
redoName() ) );
189 redoMenuAction_->setEnabled(
true);
190 redoToolAction_->setText( tr(
"Redo '%1'").arg( globalBackup_.
redoName() ) );
191 redoToolAction_->setEnabled(
true);
193 redoMenuAction_->setText( tr(
"Redo") );
194 redoMenuAction_->setEnabled(
false);
195 redoToolAction_->setText( tr(
"Redo") );
196 redoToolAction_->setEnabled(
false);
199 backupsEnabledAction_->setChecked( OpenFlipper::Options::backupEnabled() );
205 OpenFlipper::Options::enableBackup(backupsEnabledAction_->isChecked());
206 std::cerr <<
"Setting OpenFlipper Option 1 " << backupsEnabledAction_->isChecked() << std::endl;
212void BackupPlugin::slotKeyEvent( QKeyEvent* _event ){
213 switch (_event->key())
216 if ( _event->modifiers() == Qt::ControlModifier )
218 else if ( _event->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier) )
228void BackupPlugin::objectDeleted(
int _objectid) {
235void BackupPlugin::slotUpdateContextMenu(
int _objectId ){
238 undoContextAction_->setText( tr(
"Undo") );
239 undoContextAction_->setEnabled(
false);
240 redoContextAction_->setText( tr(
"Redo") );
241 redoContextAction_->setEnabled(
false);
255 undoContextAction_->setData(_objectId);
256 undoContextAction_->setText( tr(
"Undo '%1'").arg( backupData->
undoName() ) );
257 undoContextAction_->setEnabled( !backupData->
undoBlocked() );
261 redoContextAction_->setData(_objectId);
262 redoContextAction_->setText( tr(
"Redo '%1'").arg( backupData->
redoName() ) );
263 redoContextAction_->setEnabled( !backupData->
redoBlocked() );
272void BackupPlugin::slotObjectUndo(){
274 int id = undoContextAction_->data().toInt();
280void BackupPlugin::slotObjectRedo(){
282 int id = undoContextAction_->data().toInt();
288void BackupPlugin::slotCreateBackup(
int _objectid, QString _name,
UpdateType _type){
290 if ( !OpenFlipper::Options::backupEnabled() )
296 emit log(
LOGWARN,
"Unable to find backup object with id " + QString::number(_objectid));
301 emit generateBackup( _objectid, _name, _type );
305 groupIds.push_back( _objectid );
307 bool skipBackup =
false;
313 if ( backupData != 0 )
323 emit log(
LOGOUT,
"Created backup for " + QString::number(_objectid)+
" , Name : '" + _name +
"'" );
328void BackupPlugin::slotCreateBackup(
IdList _objectids , QString _name, std::vector<UpdateType> _types){
330 if ( !OpenFlipper::Options::backupEnabled() )
335 if ( _objectids.size() != _types.size() ){
336 emit log(
LOGWARN,
"Unable to create backup sizes of ids and updateTypes do not match!");
341 for (
unsigned int i=0; i < _objectids.size(); ++i){
346 emit log(
LOGWARN,
"Unable to find backup object with id " + QString::number(_objectids[i]));
351 emit generateBackup( _objectids[i], _name, _types[i] );
352 groupIds.push_back( _objectids[i] );
356 if ( ! groupIds.empty() ){
362 emit log(
LOGOUT,
"Created grouped backup, Name : '" + _name +
"'" );
368void BackupPlugin::slotUndo(
int _objectid){
370 emit aboutToRestore(_objectid);
371 globalBackup_.
undo(_objectid);
372 emit restored(_objectid);
380void BackupPlugin::slotUndo(){
384 IdList ids = group->objectIDs();
385 IdList::const_iterator it, end;
386 for (it = ids.begin(), end = ids.end(); it != end; ++it)
387 emit aboutToRestore(*it);
389 globalBackup_.
undo();
391 for (
unsigned int i=0; i < group->objectIDs().size(); i++)
393 emit restored(group->objectIDs()[i]);
394 emit updatedObject(group->objectIDs()[i],
UPDATE_ALL);
399 emit log(
LOGWARN,
"Unable to find the current GroupBackup");
404void BackupPlugin::slotRedo(
int _objectid){
406 emit aboutToRestore(_objectid);
407 globalBackup_.
redo(_objectid);
408 emit restored(_objectid);
416void BackupPlugin::slotRedo(){
417 globalBackup_.
redo();
422 for (
unsigned int i=0; i < group->objectIDs().size(); i++)
424 emit restored(group->objectIDs()[i]);
425 emit updatedObject(group->objectIDs()[i],
UPDATE_ALL);
433void BackupPlugin::createBackup(
int _objectId,
const QString& _name,
UpdateType _type)
435 slotCreateBackup(_objectId,_name,_type);
440bool BackupPlugin::initializeOptionsWidget(QWidget*& _widget)
442 QLabel* maxBackupLabel =
new QLabel();
443 maxBackupLabel->setText(tr(
"Max. saved backups: "));
444 maxBackupSpinBox_ =
new QSpinBox();
445 maxBackupSpinBox_->setValue(globalBackup_.
maxBackups());
446 maxBackupSpinBox_->setRange(0,100);
448 QHBoxLayout* layout =
new QHBoxLayout();
449 layout->addWidget(maxBackupLabel);
450 layout->addWidget(maxBackupSpinBox_);
452 QWidget* baseWidget =
new QWidget();
453 baseWidget->setLayout(layout);
454 _widget = baseWidget;
461void BackupPlugin::applyOptions()
463 int maxBackups = maxBackupSpinBox_->value();
468void BackupPlugin::createBackupSimple(
int _objectId,
const QString& _name) {
469 createBackup(_objectId, _name);
const DataType DATA_ALL(UINT_MAX)
Identifier for all available objects.
std::vector< int > IdList
Standard Type for id Lists used for scripting.
DLLEXPORT OpenFlipperQSettings & OpenFlipperSettings()
QSettings object containing all program settings of OpenFlipper.
Abstract class that is used to store backups.
void setMaxBackups(size_t _max)
set the maximum of saved backups
size_t maxBackups()
return the maximum of backups which are saved
BaseBackup * currentState()
return the current state
bool undoAvailable()
return if an undo backup is available
void storeBackup(BaseBackup *_backup)
store a backup
bool redoAvailable()
return if a redo backup is available
void clear()
remove all backups
bool redoBlocked()
return if a redo backup is blocked
bool undoBlocked()
return if an undo backup is blocked
QString redoName()
return the name of the next redo backup
QString undoName()
return the name of the next undo backup
void slotEnableDisableBackups()
Backups enabled or disabled checkbox.
QMenu * backupMenu_
The backup Menu.
bool hasObjectData(QString _dataName)
Checks if object data with given name is available.
Class that encapsulates simultaneous backups on multiple objects.
void undo()
perform an undo if possible
void eraseBackups(int _objectid)
erase all backups containing given id
void redo()
perform an redo if possible
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
void setValue(const QString &key, const QVariant &value)
Wrapper function which makes it possible to enable Debugging output with -DOPENFLIPPER_SETTINGS_DEBUG...
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.