From 14ebe0d93c0eda4965d3428c97d8ed63c417efc8 Mon Sep 17 00:00:00 2001 From: mistic100 Date: Fri, 1 Mar 2013 17:21:17 +0000 Subject: [PATCH] remove useless delete (handled by Qt), clean code --- controller.cpp | 16 ++------------ controller.h | 6 ++--- errorwidget.cpp | 11 ---------- errorwidget.h | 1 - mainwidget.cpp | 37 +++++++++++++------------------ mainwidget.h | 3 +-- mainwindow.cpp | 58 ++++++++++++++++++++----------------------------- mainwindow.h | 1 - settings.cpp | 26 ++++++++++++++-------- settings.h | 1 + 10 files changed, 62 insertions(+), 98 deletions(-) diff --git a/controller.cpp b/controller.cpp index 61722f8..db963e0 100644 --- a/controller.cpp +++ b/controller.cpp @@ -17,20 +17,11 @@ Controller::Controller(Settings* _data) : QObject(0) srand(time(NULL)); m_poSettings = _data; - m_poMainTimer = new QTimer(); - m_poMainWidget = NULL; + m_poMainTimer = new QTimer(this); connect(m_poMainTimer, SIGNAL(timeout()), this, SLOT(vSlotUpdate())); } -/* - * destructor - */ -Controller::~Controller() -{ - delete m_poMainTimer; -} - /* * update the wallpaper and start the timer */ @@ -68,10 +59,7 @@ void Controller::vSlotUpdate(bool _forcecheck) { m_poSettings->vUpdateSets(); m_poSettings->vReadNbWalls(); - if (m_poMainWidget != NULL) - { - m_poMainWidget->vUpdateList(); - } + emit listChanged(); } int iTotalFiles = m_poSettings->iNbFiles(); diff --git a/controller.h b/controller.h index 25f55c5..812f4d6 100644 --- a/controller.h +++ b/controller.h @@ -17,17 +17,13 @@ class Controller : public QObject private: Settings* m_poSettings; - MainWidget* m_poMainWidget; QTimer* m_poMainTimer; public: Controller(Settings* _data); - ~Controller(); Settings* const &settings() { return m_poSettings; } - void vSetWidget(MainWidget* _w) { m_poMainWidget=_w; } - bool const bIsRunning() { return m_poMainTimer->isActive(); } void vStartTimer(bool _forcecheck=false); void vStartPause(); @@ -38,6 +34,8 @@ class Controller : public QObject public slots: void vSlotUpdate(bool _forcecheck=false); +signals: + void listChanged(); }; #endif // CONTROLLER_H diff --git a/errorwidget.cpp b/errorwidget.cpp index 6f8d4f1..b7ac1de 100644 --- a/errorwidget.cpp +++ b/errorwidget.cpp @@ -76,17 +76,6 @@ ErrorWidget::ErrorWidget(QWidget* _parent, Controller* _poCtrl) : QWidget(_paren setLayout(poMainLayout); } -/* - * destructor - */ -ErrorWidget::~ErrorWidget() -{ - if (m_poEditPath!=NULL) - { - delete m_poEditPath; - } -} - /* * open file dialog */ diff --git a/errorwidget.h b/errorwidget.h index 3e93aa1..e88a196 100644 --- a/errorwidget.h +++ b/errorwidget.h @@ -15,7 +15,6 @@ class ErrorWidget : public QWidget public: ErrorWidget(QWidget* _parent=0, Controller* _poCtrl=0); - ~ErrorWidget(); public slots: void vSlotBrowse(); diff --git a/mainwidget.cpp b/mainwidget.cpp index ac0b62a..ff525a0 100644 --- a/mainwidget.cpp +++ b/mainwidget.cpp @@ -16,7 +16,6 @@ MainWidget::MainWidget(QWidget* _parent, Controller* _poCtrl) : QWidget(_parent) { m_poCtrl = _poCtrl; - m_poCtrl->vSetWidget(this); // main list m_poList = new QListWidget(); @@ -58,36 +57,29 @@ MainWidget::MainWidget(QWidget* _parent, Controller* _poCtrl) : QWidget(_parent) poMainLayout->addLayout(poRightCollumn, 1, 1); - // events - connect(poAddButton, SIGNAL(clicked()), this, SLOT(vSlotAddSet())); - connect(m_poActivateButton, SIGNAL(clicked()), this, SLOT(vSlotActivateSets())); - connect(m_poUnactivateButton, SIGNAL(clicked()), this, SLOT(vSlotUnactivateSets())); - connect(m_poDeleteButton, SIGNAL(clicked()), this, SLOT(vSlotDeleteSets())); - connect(poApplyButton, SIGNAL(clicked()), _parent, SLOT(vSlotApply())); + // buttons events + connect(poAddButton, SIGNAL(clicked()), this, SLOT(vSlotAddSet())); + connect(m_poActivateButton, SIGNAL(clicked()), this, SLOT(vSlotActivateSets())); + connect(m_poUnactivateButton, SIGNAL(clicked()), this, SLOT(vSlotUnactivateSets())); + connect(m_poDeleteButton, SIGNAL(clicked()), this, SLOT(vSlotDeleteSets())); + connect(poApplyButton, SIGNAL(clicked()), _parent, SLOT(vSlotApply())); + // list events connect(m_poList, SIGNAL(itemSelectionChanged()), this, SLOT(vSlotSelectionChanged())); connect(m_poList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(vSlotItemDoubleClicked())); + // spin box changed connect(m_poDelayInput, SIGNAL(valueChanged(int)), this, SLOT(vSlotDelayChanged(int))); + // list updated by the controller + connect(m_poCtrl, SIGNAL(listChanged()), this, SLOT(vUpdateList())); + setLayout(poMainLayout); vUpdateList(); } -/* - * destructor - */ -MainWidget::~MainWidget() -{ - delete m_poDeleteButton; - delete m_poActivateButton; - delete m_poUnactivateButton; - delete m_poDelayInput; - delete m_poList; -} - /* * update list widget contents */ @@ -150,7 +142,8 @@ void MainWidget::vSlotAddSet() */ void MainWidget::vSlotDeleteSets() { - int ret = QMessageBox::warning(this, tr("Delete"), tr("Are you sure?"), QMessageBox::Cancel | QMessageBox::Ok, QMessageBox::Cancel); + int ret = QMessageBox::warning(this, tr("Delete"), tr("Are you sure?"), + QMessageBox::Cancel | QMessageBox::Ok, QMessageBox::Cancel); if (ret == QMessageBox::Ok) { @@ -168,7 +161,7 @@ void MainWidget::vSlotActivateSets() for (QList::iterator i=list.begin(); i!=list.end(); i++) { - m_poCtrl->settings()->poGetSet(*i)->vSetActive(true); + m_poCtrl->settings()->vSetState(*i, true); QListWidgetItem* poItem = m_poList->item(*i); poItem->setData(Qt::UserRole+2, true); vSetListItemIcon(poItem, true); @@ -187,7 +180,7 @@ void MainWidget::vSlotUnactivateSets() for (QList::iterator i=list.begin(); i!=list.end(); i++) { - m_poCtrl->settings()->poGetSet(*i)->vSetActive(false); + m_poCtrl->settings()->vSetState(*i, false); QListWidgetItem* poItem = m_poList->item(*i); poItem->setData(Qt::UserRole+2, false); vSetListItemIcon(poItem, false); diff --git a/mainwidget.h b/mainwidget.h index f015cca..e8f6cbe 100644 --- a/mainwidget.h +++ b/mainwidget.h @@ -26,13 +26,12 @@ class MainWidget : public QWidget public: MainWidget(QWidget* _parent=0, Controller* _poCtrl=0); - ~MainWidget(); - void vUpdateList(); void vSetListItemIcon(QListWidgetItem* _poItem, bool _active); QList oGetSelectedIndexes(); public slots: + void vUpdateList(); void vSlotAddSet(); void vSlotDeleteSets(); void vSlotActivateSets(); diff --git a/mainwindow.cpp b/mainwindow.cpp index 959ca14..f3150e3 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -39,17 +39,6 @@ MainWindow::MainWindow(Controller* _oCtrl) : QMainWindow(0) } } -/* - * destructor - */ -MainWindow::~MainWindow() -{ - if (m_poCentralWidget != NULL) delete m_poCentralWidget; - if (m_poStatusBar != NULL) delete m_poStatusBar; - if (m_poMenuBar != NULL) delete m_poMenuBar; - if (m_poTrayIcon != NULL) delete m_poTrayIcon; -} - /* * init window, called in the constructor and by ErrorWidget */ @@ -117,25 +106,26 @@ void MainWindow::vInit() QAction* poOptionMinimize = m_poOptionsMenu->addAction(tr("Minimize on startup")); QAction* poOptionCheck = m_poOptionsMenu->addAction(tr("Check files periodically")); + QAction* poOptionAutostart = m_poOptionsMenu->addAction(tr("Start with Windows")); poOptionMinimize->setCheckable(true); poOptionMinimize->setChecked(m_poCtrl->settings()->bMinimize()); poOptionCheck->setCheckable(true); poOptionCheck->setChecked(m_poCtrl->settings()->bCheckFiles()); - - connect(poActionQuit1, SIGNAL(triggered()), this, SLOT(vSlotQuit())); - connect(m_poActionHide1, SIGNAL(triggered()), this, SLOT(vSlotToggleWindow())); - connect(m_poActionPause1, SIGNAL(triggered()), this, SLOT(vSlotStartPause())); - connect(poActionHelp, SIGNAL(triggered()), this, SLOT(vSlotShowHelp())); - connect(poOptionMinimize, SIGNAL(toggled(bool)), this, SLOT(vSlotOptionMinimizeChanged(bool))); - connect(poOptionCheck, SIGNAL(toggled(bool)), this, SLOT(vSlotOptionCheckChanged(bool))); - - if (m_poCtrl->settings()->bCanAddShortcut()) + poOptionAutostart->setCheckable(true); + poOptionAutostart->setChecked(m_poCtrl->settings()->bIsAutostart()); + + connect(poActionQuit1, SIGNAL(triggered()), this, SLOT(vSlotQuit())); + connect(m_poActionHide1, SIGNAL(triggered()), this, SLOT(vSlotToggleWindow())); + connect(m_poActionPause1, SIGNAL(triggered()), this, SLOT(vSlotStartPause())); + connect(poActionHelp, SIGNAL(triggered()), this, SLOT(vSlotShowHelp())); + connect(poOptionMinimize, SIGNAL(toggled(bool)), this, SLOT(vSlotOptionMinimizeChanged(bool))); + connect(poOptionCheck, SIGNAL(toggled(bool)), this, SLOT(vSlotOptionCheckChanged(bool))); + connect(poOptionAutostart, SIGNAL(toggled(bool)), this, SLOT(vSlotOptionAutostartChanged(bool))); + + if (!(m_poCtrl->settings()->bCanAddShortcut())) { - QAction* poOptionAutostart = m_poOptionsMenu->addAction(tr("Start with Windows")); - poOptionAutostart->setCheckable(true); - poOptionAutostart->setChecked(m_poCtrl->settings()->bIsAutostart()); - connect(poOptionAutostart, SIGNAL(toggled(bool)), this, SLOT(vSlotOptionAutostartChanged(bool))); + poOptionAutostart->setVisible(false); } setMenuBar(m_poMenuBar); @@ -158,7 +148,6 @@ void MainWindow::vInit() if (m_poTrayIcon == NULL && iState == UM_OK) { m_poTrayIcon = new QSystemTrayIcon(QIcon(":/img/icon"), this); - m_poTrayIcon->setToolTip(APP_NAME); connect(m_poTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(vSlotTrayAction(QSystemTrayIcon::ActivationReason))); @@ -173,11 +162,12 @@ void MainWindow::vInit() poActionRefresh->setIcon(QPixmap(":/img/refresh")); m_poActionHide2->setIcon(QPixmap(":/img/hide")); - connect(poActionQuit2, SIGNAL(triggered()), this, SLOT(vSlotQuit())); - connect(m_poActionHide2, SIGNAL(triggered()), this, SLOT(vSlotToggleWindow())); - connect(m_poActionPause2, SIGNAL(triggered()), this, SLOT(vSlotStartPause())); - connect(poActionRefresh, SIGNAL(triggered()), this, SLOT(vSlotApply())); + connect(poActionQuit2, SIGNAL(triggered()), this, SLOT(vSlotQuit())); + connect(m_poActionHide2, SIGNAL(triggered()), this, SLOT(vSlotToggleWindow())); + connect(m_poActionPause2, SIGNAL(triggered()), this, SLOT(vSlotStartPause())); + connect(poActionRefresh, SIGNAL(triggered()), this, SLOT(vSlotApply())); + m_poTrayIcon->setToolTip(APP_NAME); m_poTrayIcon->setContextMenu(poTrayMenu); m_poTrayIcon->show(); } @@ -203,16 +193,16 @@ void MainWindow::vSlotToggleWindow(bool _forcehide) { hide(); - m_poCtrl->settings()->vSetWindowSize(size()); - m_poCtrl->settings()->vWriteXML(); - - m_poActionHide2->setText(tr("Show")); - if (m_poCtrl->settings()->iMsgCount() < 3) { m_poTrayIcon->showMessage(APP_NAME, tr("%1 is still running").arg(APP_NAME)); m_poCtrl->settings()->vAddMsgCount(); } + + m_poCtrl->settings()->vSetWindowSize(size()); + m_poCtrl->settings()->vWriteXML(); + + m_poActionHide2->setText(tr("Show")); } else { diff --git a/mainwindow.h b/mainwindow.h index 0e75871..8fbb035 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -32,7 +32,6 @@ class MainWindow : public QMainWindow public: MainWindow(Controller* _oCtrl); - ~MainWindow(); void vInit(); diff --git a/settings.cpp b/settings.cpp index 10d467c..2629eed 100644 --- a/settings.cpp +++ b/settings.cpp @@ -486,22 +486,31 @@ QString Settings::sRenameSet(short int _i, const QString &_name, bool _returnFul return ""; } +/* + * set the state of a set + */ +void Settings::vSetState(short int _i, bool _a) +{ + m_bUnsaved = true; + m_oSets.at(_i)->vSetActive(_a); +} + /* * remove unexisting sets and update file lists */ void Settings::vUpdateSets() { - for (int i=0; i::iterator it=m_oSets.begin(); it!=m_oSets.end(); ) { - Set* poSet = m_oSets.at(i); - if (!bDirectoryExists(poSet->path().toStdString())) + if (!bDirectoryExists((*it)->path().toStdString())) { - vDeleteSet(i); - i--; + m_bUnsaved = true; + m_oSets.erase(it); } else { - poSet->vPopulateFiles(); + (*it)->vPopulateFiles(); + it++; } } } @@ -514,10 +523,9 @@ int const Settings::iNbFiles() int iTotalFiles = 0; for (QVector::iterator it=m_oSets.begin(); it!=m_oSets.end(); ++it) { - Set* poSet = *it; - if (poSet->isActive()) + if ((*it)->isActive()) { - iTotalFiles+= poSet->count(); + iTotalFiles+= (*it)->count(); } } return iTotalFiles; diff --git a/settings.h b/settings.h index d6e95ff..679f1b7 100644 --- a/settings.h +++ b/settings.h @@ -71,6 +71,7 @@ class Settings void vDeleteSets(const QList &_list); void vDeleteAll(); QString sRenameSet(short int _i, const QString &_name, bool _returnFull); + void vSetState(short int _i, bool _a); void vUpdateSets(); void vCreateShortcut();