MainWindow: use non-pointer members where possible
authorTobias Doerffel <tobias.doerffel@gmail.com>
Sat, 7 Aug 2010 10:04:45 +0000 (12:04 +0200)
committerDaniel Klaffenbach <danielklaffenbach@gmail.com>
Sun, 8 Aug 2010 20:05:56 +0000 (22:05 +0200)
Where possible let the compiler manage construction/destruction of
objects as this minimizes sources of memory leaks and allows more
easy instantiation.

Signed-off-by: Daniel Klaffenbach <danielklaffenbach@gmail.com>

src/mainwindow.cpp
src/mainwindow.h

index 67d03ed..15d56fa 100755 (executable)
 
 MainWindow::MainWindow(QWidget *parent) :
     QMainWindow(parent),
-    ui(new Ui::MainWindow)
+    ui(new Ui::MainWindow),
+    //create helper process
+    helperProcess( this ),
+    //create a new, stackable help window
+    helpWindow( this ),
+    //create UI refresh timer
+    refreshTimer( this ),
+    //create a QGraphicsScene for the little chip icon
+    scene( this ),
+    //show errors about the sudo setup only once
+    showSudoError( true )
 {
     //this is a stacked window on Maemo 5
     #if defined(Q_WS_MAEMO_5)
@@ -47,27 +57,17 @@ MainWindow::MainWindow(QWidget *parent) :
     // enable auto rotation
     setAutoRotation();
 
-    //create a QGraphicsScene for the little chip icon
-    scene = new QGraphicsScene();
+    //initialize orientation
     orientationChanged();
 
-    //create the refresh timer
-    refreshTimer = new QTimer();
     //refresh UI every 10 seconds
-    refreshTimer->start( 10000 );
+    refreshTimer.start( 10000 );
 
-    //create helper process
-    helperProcess = new QProcess;
-
-    //create a new, stackable help window
-    helpWindow = new HelpWindow( this );
+    // initialize stackable help window
     #if defined(Q_WS_MAEMO_5)
-       helpWindow->setAttribute(Qt::WA_Maemo5StackedWindow);
+       helpWindow.setAttribute(Qt::WA_Maemo5StackedWindow);
     #endif
-    helpWindow->setWindowFlags( windowFlags() | Qt::Window );
-
-    //show errors about the sudo setup only once
-    showSudoError = true;
+    helpWindow.setWindowFlags( windowFlags() | Qt::Window );
 
     //connect signals and slots
     connect(ui->actionHelp, SIGNAL(triggered()), this, SLOT(showHelp()));
@@ -75,15 +75,12 @@ MainWindow::MainWindow(QWidget *parent) :
     connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
     connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
     connect(ui->sr_box, SIGNAL(clicked()), this, SLOT(setSmartReflex()));
-    connect(refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
+    connect(&refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
 
 }
 
 MainWindow::~MainWindow()
 {
-    delete helpWindow;
-    delete refreshTimer;
-    delete scene;
     delete ui;
 }
 
@@ -139,15 +136,15 @@ int MainWindow::callHelper(QString action, QString param)
     arguments.append( action );
     arguments.append( param );
 
-    helperProcess->start( "sudo", arguments, QIODevice::NotOpen );
+    helperProcess.start( "sudo", arguments, QIODevice::NotOpen );
 
-    if ( showSudoError && !helperProcess->waitForFinished( 400 )) {
+    if ( showSudoError && !helperProcess.waitForFinished( 400 )) {
        //do not show this error again
        showSudoError = false;
        QMessageBox::critical(this, tr("QCPUFreq"), tr("There seems to be a problem with your sudo setup!"));
     }
 
-    return helperProcess->exitCode();
+    return helperProcess.exitCode();
 }
 
 
@@ -362,18 +359,18 @@ void MainWindow::orientationChanged()
     if ( usePortrait() ) {
        //in portrait mode we want to display the large image
        image.load( ":/img/chip256" );
-       this->scene->clear();
-       this->scene->addPixmap(  image  );
+       scene.clear();
+       scene.addPixmap(  image  );
 
-       ui->graphicsPortrait->setScene( this->scene );
+       ui->graphicsPortrait->setScene( &scene );
        ui->graphicsPortrait->setMaximumSize( 256, 256 );
        ui->graphicsLandscape->setMaximumSize( 0, 0 );
     } else {
        image.load( ":/img/chip128" );
-       this->scene->clear();
-       this->scene->addPixmap(  image  );
+       scene.clear();
+       scene.addPixmap(  image  );
 
-       ui->graphicsLandscape->setScene( this->scene );
+       ui->graphicsLandscape->setScene( &scene );
        ui->graphicsLandscape->setMaximumSize( 128, 128 );
        ui->graphicsPortrait->setMaximumSize( 0, 0 );
     }
@@ -416,7 +413,7 @@ void MainWindow::setSmartReflex()
   */
 void MainWindow::showHelp()
 {
-    helpWindow->show();
+    helpWindow.show();
 }
 
 
index 9aa4763..7f10e82 100755 (executable)
@@ -60,14 +60,14 @@ private:
     int getScalingSteps();
     int getSmartReflexState();
     //! The helper process
-    QProcess *helperProcess;
+    QProcess helperProcess;
     //! The help window
-    HelpWindow *helpWindow;
+    HelpWindow helpWindow;
     QString readSysFile( QString sys_file );
     //! the timer for refreshing the UI
-    QTimer *refreshTimer;
+    QTimer refreshTimer;
     //! the QGraphicsScene will contain the large chip icon displayed in the UI
-    QGraphicsScene *scene;
+    QGraphicsScene scene;
     bool showSudoError;
     bool usePortrait();
 };