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);
77 std::cerr <<
"Setting OpenFlipper Option 2 " <<
OpenFlipperSettings().
value(
"BackupPlugin/EnableBackups",
true).toBool() << std::endl;
78 OpenFlipper::Options::enableBackup(
OpenFlipperSettings().value(
"BackupPlugin/EnableBackups",
true).toBool());
83 void BackupPlugin::pluginsInitialized() {
86 emit getMenubarMenu(tr(
"&Backup"), backupMenu_,
true );
88 if ( OpenFlipper::Options::gui() ) {
90 backupsEnabledAction_ =
new QAction(
"Backups Enabled",0);
91 backupsEnabledAction_->setCheckable(
true);
92 backupsEnabledAction_->setChecked(OpenFlipper::Options::backupEnabled());
93 connect (backupsEnabledAction_, SIGNAL(triggered(
bool)),
this, SLOT(slotEnableDisableBackups()) );
95 undoMenuAction_ =
new QAction(tr(
"&Undo"),
this);
96 undoMenuAction_->setEnabled(
false);
97 undoMenuAction_->setStatusTip(tr(
"Undo the last action."));
98 undoMenuAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-undo.png") );
99 connect(undoMenuAction_, SIGNAL(triggered()),
this, SIGNAL( undo() ) );
101 redoMenuAction_ =
new QAction(tr(
"&Redo"),
this);
102 redoMenuAction_->setEnabled(
false);
103 redoMenuAction_->setStatusTip(tr(
"Redo the last action"));
104 redoMenuAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-redo.png") );
105 connect(redoMenuAction_, SIGNAL(triggered()),
this, SIGNAL( redo() ) );
107 backupMenu_->addAction(undoMenuAction_);
108 backupMenu_->addAction(redoMenuAction_);
109 backupMenu_->addSeparator();
110 backupMenu_->addAction(backupsEnabledAction_);
113 QToolBar* toolbar =
new QToolBar(
"Backup Toolbar");
116 undoToolAction_ =
new QAction(tr(
"&Undo"),
this);
117 undoToolAction_->setEnabled(
false);
118 undoToolAction_->setStatusTip(tr(
"Undo the last action."));
119 undoToolAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-undo.png") );
120 connect(undoToolAction_, SIGNAL(triggered()),
this, SIGNAL( undo() ) );
121 toolbar->addAction(undoToolAction_);
124 redoToolAction_ =
new QAction(tr(
"&Redo"),
this);
125 redoToolAction_->setEnabled(
false);
126 redoToolAction_->setStatusTip(tr(
"Redo the last action"));
127 redoToolAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-redo.png") );
128 connect(redoToolAction_, SIGNAL(triggered()),
this, SIGNAL( redo() ) );
129 toolbar->addAction(redoToolAction_);
131 emit addToolbar( toolbar );
134 emit registerKey(Qt::Key_Z, Qt::ControlModifier, tr(
"Undo Action"));
135 emit registerKey(Qt::Key_Z, (Qt::ControlModifier | Qt::ShiftModifier), tr(
"Redo Action"));
138 undoContextAction_ =
new QAction(tr(
"&Undo"),
this);
139 undoContextAction_->setEnabled(
false);
140 undoContextAction_->setStatusTip(tr(
"Undo the last action."));
141 undoContextAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-undo.png") );
142 connect(undoContextAction_, SIGNAL(triggered()),
this, SLOT(slotObjectUndo()) );
145 redoContextAction_ =
new QAction(tr(
"&Redo"),
this);
146 redoContextAction_->setEnabled(
false);
147 redoContextAction_->setStatusTip(tr(
"Redo the last action"));
148 redoContextAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"edit-redo.png") );
149 connect(redoContextAction_, SIGNAL(triggered()),
this, SLOT(slotObjectRedo()) );
153 if (maxBackupSpinBox_)
154 maxBackupSpinBox_->setValue(globalBackup_.maxBackups());
159 emit setSlotDescription(
"createBackup(int,QString)", tr(
"Creates a backup which can be restored via undo."),
160 QString(
"objectId,name").split(
","), QString(
"Id of the object,name of the backup").split(
","));
161 emit setSlotDescription(
"createBackup(int,QString,UpdateType)", tr(
"Creates a backup which can be restored via undo."),
162 QString(
"objectId,name,type").split(
","), QString(
"Id of the object,name of the backup,updatetypes which are changed").split(
","));
167 void BackupPlugin::slotAllCleared(){
168 globalBackup_.clear();
174 void BackupPlugin::updateButtons() {
176 if ( globalBackup_.undoAvailable() ){
177 undoMenuAction_->setText( tr(
"Undo '%1'").arg( globalBackup_.undoName() ) );
178 undoMenuAction_->setEnabled(
true);
179 undoToolAction_->setText( tr(
"Undo '%1'").arg( globalBackup_.undoName() ) );
180 undoToolAction_->setEnabled(
true);
182 undoMenuAction_->setText( tr(
"Undo") );
183 undoMenuAction_->setEnabled(
false);
184 undoToolAction_->setText( tr(
"Undo") );
185 undoToolAction_->setEnabled(
false);
188 if ( globalBackup_.redoAvailable() ){
189 redoMenuAction_->setText( tr(
"Redo '%1'").arg( globalBackup_.redoName() ) );
190 redoMenuAction_->setEnabled(
true);
191 redoToolAction_->setText( tr(
"Redo '%1'").arg( globalBackup_.redoName() ) );
192 redoToolAction_->setEnabled(
true);
194 redoMenuAction_->setText( tr(
"Redo") );
195 redoMenuAction_->setEnabled(
false);
196 redoToolAction_->setText( tr(
"Redo") );
197 redoToolAction_->setEnabled(
false);
200 backupsEnabledAction_->setChecked( OpenFlipper::Options::backupEnabled() );
206 OpenFlipper::Options::enableBackup(backupsEnabledAction_->isChecked());
207 std::cerr <<
"Setting OpenFlipper Option 1 " << backupsEnabledAction_->isChecked() << std::endl;
213 void BackupPlugin::slotKeyEvent( QKeyEvent* _event ){
214 switch (_event->key())
217 if ( _event->modifiers() == Qt::ControlModifier )
219 else if ( _event->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier) )
229 void BackupPlugin::objectDeleted(
int _objectid) {
230 globalBackup_.eraseBackups(_objectid);
236 void BackupPlugin::slotUpdateContextMenu(
int _objectId ){
239 undoContextAction_->setText( tr(
"Undo") );
240 undoContextAction_->setEnabled(
false);
241 redoContextAction_->setText( tr(
"Redo") );
242 redoContextAction_->setEnabled(
false);
256 undoContextAction_->setData(_objectId);
257 undoContextAction_->setText( tr(
"Undo '%1'").arg( backupData->
undoName() ) );
258 undoContextAction_->setEnabled( !backupData->
undoBlocked() );
262 redoContextAction_->setData(_objectId);
263 redoContextAction_->setText( tr(
"Redo '%1'").arg( backupData->
redoName() ) );
264 redoContextAction_->setEnabled( !backupData->
redoBlocked() );
273 void BackupPlugin::slotObjectUndo(){
275 int id = undoContextAction_->data().toInt();
281 void BackupPlugin::slotObjectRedo(){
283 int id = undoContextAction_->data().toInt();
289 void BackupPlugin::slotCreateBackup(
int _objectid, QString _name,
UpdateType _type){
291 if ( !OpenFlipper::Options::backupEnabled() )
297 emit log(
LOGWARN,
"Unable to find backup object with id " + QString::number(_objectid));
302 emit generateBackup( _objectid, _name, _type );
306 groupIds.push_back( _objectid );
308 bool skipBackup =
false;
310 if ( globalBackup_.undoAvailable() ){
314 if ( backupData != 0 )
320 globalBackup_.storeBackup( backup );
324 emit log(
LOGOUT,
"Created backup for " + QString::number(_objectid)+
" , Name : '" + _name +
"'" );
329 void BackupPlugin::slotCreateBackup(
IdList _objectids , QString _name, std::vector<UpdateType> _types){
331 if ( !OpenFlipper::Options::backupEnabled() )
336 if ( _objectids.size() != _types.size() ){
337 emit log(
LOGWARN,
"Unable to create backup sizes of ids and updateTypes do not match!");
342 for (
unsigned int i=0; i < _objectids.size(); ++i){
347 emit log(
LOGWARN,
"Unable to find backup object with id " + QString::number(_objectids[i]));
352 emit generateBackup( _objectids[i], _name, _types[i] );
353 groupIds.push_back( _objectids[i] );
357 if ( ! groupIds.empty() ){
360 globalBackup_.storeBackup( backup );
363 emit log(
LOGOUT,
"Created grouped backup, Name : '" + _name +
"'" );
369 void BackupPlugin::slotUndo(
int _objectid){
371 emit aboutToRestore(_objectid);
372 globalBackup_.undo(_objectid);
373 emit restored(_objectid);
381 void BackupPlugin::slotUndo(){
385 IdList ids = group->objectIDs();
386 IdList::const_iterator it, end;
387 for (it = ids.begin(), end = ids.end(); it != end; ++it)
388 emit aboutToRestore(*it);
390 globalBackup_.undo();
392 for (
unsigned int i=0; i < group->objectIDs().size(); i++)
394 emit restored(group->objectIDs()[i]);
395 emit updatedObject(group->objectIDs()[i],
UPDATE_ALL);
400 emit log(
LOGWARN,
"Unable to find the current GroupBackup");
405 void BackupPlugin::slotRedo(
int _objectid){
407 emit aboutToRestore(_objectid);
408 globalBackup_.redo(_objectid);
409 emit restored(_objectid);
417 void BackupPlugin::slotRedo(){
418 globalBackup_.redo();
423 for (
unsigned int i=0; i < group->objectIDs().size(); i++)
425 emit restored(group->objectIDs()[i]);
426 emit updatedObject(group->objectIDs()[i],
UPDATE_ALL);
436 slotCreateBackup(_objectId,_name,_type);
441 bool BackupPlugin::initializeOptionsWidget(QWidget*& _widget)
443 QLabel* maxBackupLabel =
new QLabel();
444 maxBackupLabel->setText(tr(
"Max. saved backups: "));
445 maxBackupSpinBox_ =
new QSpinBox();
446 maxBackupSpinBox_->setValue(globalBackup_.maxBackups());
447 maxBackupSpinBox_->setRange(0,100);
449 QHBoxLayout* layout =
new QHBoxLayout();
450 layout->addWidget(maxBackupLabel);
451 layout->addWidget(maxBackupSpinBox_);
453 QWidget* baseWidget =
new QWidget();
454 baseWidget->setLayout(layout);
455 _widget = baseWidget;
462 void BackupPlugin::applyOptions()
464 int maxBackups = maxBackupSpinBox_->value();
465 globalBackup_.setMaxBackups(maxBackups);
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
bool redoAvailable()
return if a redo backup is available
bool undoBlocked()
return if an undo backup is blocked
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
QString redoName()
return the name of the next redo backup
Class that encapsulates simultaneous backups on multiple objects.
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Abstract class that is used to store backups.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
void createBackup(int _objectId, QString _name, UpdateType _type=UPDATE_ALL)
Tell Backup Plugin to create a backup.
void slotEnableDisableBackups()
Backups enabled or disabled checkbox.
bool redoBlocked()
return if a redo backup is blocked
QString undoName()
return the name of the next undo backup
bool undoAvailable()
return if an undo backup is available
void setValue(const QString &key, const QVariant &value)
Wrapper function which makes it possible to enable Debugging output with -DOPENFLIPPER_SETTINGS_DEBUG...
bool hasObjectData(QString _dataName)
Checks if object data with given name is available.
DLLEXPORT OpenFlipperQSettings & OpenFlipperSettings()
QSettings object containing all program settings of OpenFlipper.
const DataType DATA_ALL(UINT_MAX)
Identifier for all available objects.