44 #include "BackupPlugin.hh" 49 #include "GroupBackup.hh" 52 #include <QHBoxLayout> 55 BackupPlugin::BackupPlugin():
58 backupsEnabledAction_(0),
63 undoContextAction_(0),
64 redoContextAction_(0),
72 void BackupPlugin::initializePlugin()
74 int maxBackups =
OpenFlipperSettings().
value(
"BackupPlugin/MaxBackups",static_cast<unsigned>(globalBackup_.maxBackups())).toInt();
75 globalBackup_.setMaxBackups(maxBackups);
80 void BackupPlugin::pluginsInitialized() {
83 emit getMenubarMenu(tr(
"&Backup"), backupMenu_,
true );
85 if ( OpenFlipper::Options::gui() ) {
87 backupsEnabledAction_ =
new QAction(
"Backups Enabled",0);
88 backupsEnabledAction_->setCheckable(
true);
89 connect (backupsEnabledAction_, SIGNAL(triggered(
bool)),
this, SLOT(slotEnableDisableBackups()) );
91 undoMenuAction_ =
new QAction(tr(
"&Undo"),
this);
92 undoMenuAction_->setEnabled(
false);
93 undoMenuAction_->setStatusTip(tr(
"Undo the last action."));
94 undoMenuAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-undo.png") );
95 connect(undoMenuAction_, SIGNAL(triggered()),
this, SIGNAL( undo() ) );
97 redoMenuAction_ =
new QAction(tr(
"&Redo"),
this);
98 redoMenuAction_->setEnabled(
false);
99 redoMenuAction_->setStatusTip(tr(
"Redo the last action"));
100 redoMenuAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-redo.png") );
101 connect(redoMenuAction_, SIGNAL(triggered()),
this, SIGNAL( redo() ) );
103 backupMenu_->addAction(undoMenuAction_);
104 backupMenu_->addAction(redoMenuAction_);
105 backupMenu_->addSeparator();
106 backupMenu_->addAction(backupsEnabledAction_);
109 QToolBar* toolbar =
new QToolBar(
"Backup Toolbar");
112 undoToolAction_ =
new QAction(tr(
"&Undo"),
this);
113 undoToolAction_->setEnabled(
false);
114 undoToolAction_->setStatusTip(tr(
"Undo the last action."));
115 undoToolAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-undo.png") );
116 connect(undoToolAction_, SIGNAL(triggered()),
this, SIGNAL( undo() ) );
117 toolbar->addAction(undoToolAction_);
120 redoToolAction_ =
new QAction(tr(
"&Redo"),
this);
121 redoToolAction_->setEnabled(
false);
122 redoToolAction_->setStatusTip(tr(
"Redo the last action"));
123 redoToolAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-redo.png") );
124 connect(redoToolAction_, SIGNAL(triggered()),
this, SIGNAL( redo() ) );
125 toolbar->addAction(redoToolAction_);
127 emit addToolbar( toolbar );
130 emit registerKey(Qt::Key_Z, Qt::ControlModifier, tr(
"Undo Action"));
131 emit registerKey(Qt::Key_Z, (Qt::ControlModifier | Qt::ShiftModifier), tr(
"Redo Action"));
134 undoContextAction_ =
new QAction(tr(
"&Undo"),
this);
135 undoContextAction_->setEnabled(
false);
136 undoContextAction_->setStatusTip(tr(
"Undo the last action."));
137 undoContextAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-undo.png") );
138 connect(undoContextAction_, SIGNAL(triggered()),
this, SLOT(slotObjectUndo()) );
141 redoContextAction_ =
new QAction(tr(
"&Redo"),
this);
142 redoContextAction_->setEnabled(
false);
143 redoContextAction_->setStatusTip(tr(
"Redo the last action"));
144 redoContextAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-redo.png") );
145 connect(redoContextAction_, SIGNAL(triggered()),
this, SLOT(slotObjectRedo()) );
149 if (maxBackupSpinBox_)
150 maxBackupSpinBox_->setValue(globalBackup_.maxBackups());
155 emit setSlotDescription(
"createBackup(int,QString)", tr(
"Creates a backup which can be restored via undo."),
156 QString(
"objectId,name").split(
","), QString(
"Id of the object,name of the backup").split(
","));
157 emit setSlotDescription(
"createBackup(int,QString,UpdateType)", tr(
"Creates a backup which can be restored via undo."),
158 QString(
"objectId,name,type").split(
","), QString(
"Id of the object,name of the backup,updatetypes which are changed").split(
","));
163 void BackupPlugin::slotAllCleared(){
164 globalBackup_.clear();
170 void BackupPlugin::updateButtons() {
172 if ( globalBackup_.undoAvailable() ){
173 undoMenuAction_->setText( tr(
"Undo '%1'").arg( globalBackup_.undoName() ) );
174 undoMenuAction_->setEnabled(
true);
175 undoToolAction_->setText( tr(
"Undo '%1'").arg( globalBackup_.undoName() ) );
176 undoToolAction_->setEnabled(
true);
178 undoMenuAction_->setText( tr(
"Undo") );
179 undoMenuAction_->setEnabled(
false);
180 undoToolAction_->setText( tr(
"Undo") );
181 undoToolAction_->setEnabled(
false);
184 if ( globalBackup_.redoAvailable() ){
185 redoMenuAction_->setText( tr(
"Redo '%1'").arg( globalBackup_.redoName() ) );
186 redoMenuAction_->setEnabled(
true);
187 redoToolAction_->setText( tr(
"Redo '%1'").arg( globalBackup_.redoName() ) );
188 redoToolAction_->setEnabled(
true);
190 redoMenuAction_->setText( tr(
"Redo") );
191 redoMenuAction_->setEnabled(
false);
192 redoToolAction_->setText( tr(
"Redo") );
193 redoToolAction_->setEnabled(
false);
196 backupsEnabledAction_->setChecked( OpenFlipper::Options::backupEnabled() );
202 OpenFlipper::Options::enableBackup(backupsEnabledAction_->isChecked());
207 void BackupPlugin::slotKeyEvent( QKeyEvent* _event ){
208 switch (_event->key())
211 if ( _event->modifiers() == Qt::ControlModifier )
213 else if ( _event->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier) )
223 void BackupPlugin::objectDeleted(
int _objectid) {
224 globalBackup_.eraseBackups(_objectid);
230 void BackupPlugin::slotUpdateContextMenu(
int _objectId ){
233 undoContextAction_->setText( tr(
"Undo") );
234 undoContextAction_->setEnabled(
false);
235 redoContextAction_->setText( tr(
"Redo") );
236 redoContextAction_->setEnabled(
false);
250 undoContextAction_->setData(_objectId);
251 undoContextAction_->setText( tr(
"Undo '%1'").arg( backupData->
undoName() ) );
252 undoContextAction_->setEnabled( !backupData->
undoBlocked() );
256 redoContextAction_->setData(_objectId);
257 redoContextAction_->setText( tr(
"Redo '%1'").arg( backupData->
redoName() ) );
258 redoContextAction_->setEnabled( !backupData->
redoBlocked() );
267 void BackupPlugin::slotObjectUndo(){
269 int id = undoContextAction_->data().toInt();
275 void BackupPlugin::slotObjectRedo(){
277 int id = undoContextAction_->data().toInt();
283 void BackupPlugin::slotCreateBackup(
int _objectid, QString _name,
UpdateType _type){
285 if ( !OpenFlipper::Options::backupEnabled() )
291 emit log(
LOGWARN,
"Unable to find backup object with id " + QString::number(_objectid));
296 emit generateBackup( _objectid, _name, _type );
300 groupIds.push_back( _objectid );
302 bool skipBackup =
false;
304 if ( globalBackup_.undoAvailable() ){
308 if ( backupData != 0 )
314 globalBackup_.storeBackup( backup );
318 emit log(
LOGOUT,
"Created backup for " + QString::number(_objectid)+
" , Name : '" + _name +
"'" );
323 void BackupPlugin::slotCreateBackup(
IdList _objectids , QString _name, std::vector<UpdateType> _types){
325 if ( !OpenFlipper::Options::backupEnabled() )
330 if ( _objectids.size() != _types.size() ){
331 emit log(
LOGWARN,
"Unable to create backup sizes of ids and updateTypes do not match!");
336 for (
unsigned int i=0; i < _objectids.size(); ++i){
341 emit log(
LOGWARN,
"Unable to find backup object with id " + QString::number(_objectids[i]));
346 emit generateBackup( _objectids[i], _name, _types[i] );
347 groupIds.push_back( _objectids[i] );
351 if ( ! groupIds.empty() ){
354 globalBackup_.storeBackup( backup );
357 emit log(
LOGOUT,
"Created grouped backup, Name : '" + _name +
"'" );
363 void BackupPlugin::slotUndo(
int _objectid){
365 emit aboutToRestore(_objectid);
366 globalBackup_.undo(_objectid);
367 emit restored(_objectid);
375 void BackupPlugin::slotUndo(){
379 IdList ids = group->objectIDs();
380 IdList::const_iterator it, end;
381 for (it = ids.begin(), end = ids.end(); it != end; ++it)
382 emit aboutToRestore(*it);
384 globalBackup_.undo();
386 for (
unsigned int i=0; i < group->objectIDs().size(); i++)
388 emit restored(group->objectIDs()[i]);
389 emit updatedObject(group->objectIDs()[i],
UPDATE_ALL);
394 emit log(
LOGWARN,
"Unable to find the current GroupBackup");
399 void BackupPlugin::slotRedo(
int _objectid){
401 emit aboutToRestore(_objectid);
402 globalBackup_.redo(_objectid);
403 emit restored(_objectid);
411 void BackupPlugin::slotRedo(){
412 globalBackup_.redo();
417 for (
unsigned int i=0; i < group->objectIDs().size(); i++)
419 emit restored(group->objectIDs()[i]);
420 emit updatedObject(group->objectIDs()[i],
UPDATE_ALL);
430 slotCreateBackup(_objectId,_name,_type);
435 bool BackupPlugin::initializeOptionsWidget(QWidget*& _widget)
437 QLabel* maxBackupLabel =
new QLabel();
438 maxBackupLabel->setText(tr(
"Max. saved backups: "));
439 maxBackupSpinBox_ =
new QSpinBox();
440 maxBackupSpinBox_->setValue(globalBackup_.maxBackups());
441 maxBackupSpinBox_->setRange(0,100);
443 QHBoxLayout* layout =
new QHBoxLayout();
444 layout->addWidget(maxBackupLabel);
445 layout->addWidget(maxBackupSpinBox_);
447 QWidget* baseWidget =
new QWidget();
448 baseWidget->setLayout(layout);
449 _widget = baseWidget;
456 void BackupPlugin::applyOptions()
458 int maxBackups = maxBackupSpinBox_->value();
459 globalBackup_.setMaxBackups(maxBackups);
DLLEXPORT OpenFlipperQSettings & OpenFlipperSettings()
QSettings object containing all program settings of OpenFlipper.
bool redoBlocked()
return if a redo backup is blocked
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
Class that encapsulates simultaneous backups on multiple objects.
void setValue(const QString &key, const QVariant &value)
Wrapper function which makes it possible to enable Debugging output with -DOPENFLIPPER_SETTINGS_DEBUG...
QString undoName()
return the name of the next undo backup
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
void createBackup(int _objectId, QString _name, UpdateType _type=UPDATE_ALL)
Tell Backup Plugin to create a backup.
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Abstract class that is used to store backups.
QString redoName()
return the name of the next redo backup
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
bool hasObjectData(QString _dataName)
Checks if object data with given name is available.
void slotEnableDisableBackups()
Backups enabled or disabled checkbox.
bool redoAvailable()
return if a redo backup is available
bool undoAvailable()
return if an undo backup is available
const DataType DATA_ALL(UINT_MAX)
Identifier for all available objects.
The Menu will be shown when an object was picked.
bool undoBlocked()
return if an undo backup is blocked