From: Daniel Klaffenbach Date: Sat, 26 Jun 2010 21:37:14 +0000 (+0200) Subject: Initial help window support, show sudo errors. X-Git-Tag: v0.3~2 X-Git-Url: https://vcs.maemo.org/git/?p=qcpufreq;a=commitdiff_plain;h=d26db462593aa085bea4a07124bdaed1363e50d5 Initial help window support, show sudo errors. * Added a new help window which can be used for displaying help messages * Improved sudo process handling * Display an error about sudo setup --- diff --git a/src/help_en.html b/src/help_en.html new file mode 100644 index 0000000..767c67c --- /dev/null +++ b/src/help_en.html @@ -0,0 +1,7 @@ +

QCPUFreq Help

+ +This application allows you to lower your maximum CPU speed. +

+You only need to +
+fsd diff --git a/src/helpwindow.cpp b/src/helpwindow.cpp new file mode 100644 index 0000000..a4d2a3a --- /dev/null +++ b/src/helpwindow.cpp @@ -0,0 +1,59 @@ +#include "helpwindow.h" +#include "ui_helpwindow.h" + +#include +#include +#include +#include + +HelpWindow::HelpWindow(QWidget *parent) : + QWidget(parent), + ui(new Ui::HelpWindow) +{ + //this is a stacked window on Maemo 5 + #if defined(Q_WS_MAEMO_5) + //setAttribute(Qt::WA_Maemo5StackedWindow); + #endif + ui->setupUi(this); + + setHelpText(); + +} + +HelpWindow::~HelpWindow() +{ + delete ui; +} + + +/** + * Assigns the help text to the QTextEdit + */ +void HelpWindow::setHelpText() +{ + //get the current locale name for lacalized help messages + QString locale = QLocale::system().name(); + + //open help text + QFile help( ":/txt/help_" + locale ); + + //open the file + if ( !help.exists() || !help.open( QIODevice::ReadOnly ) ) { + //try to open the file in english language instead + help.setFileName(":/txt/help_en"); + if ( !help.exists() || !help.open( QIODevice::ReadOnly ) ) { + QMessageBox::critical(this, tr("QCPUFreq"), tr("Cannot open help file!")); + return; + } + } + + //read the file + QTextStream in( &help ); + QString txt; + do { + txt += in.readLine(); + txt += "\n"; + } while ( !in.atEnd() ); + + ui->textEdit->setText( txt ); +} diff --git a/src/helpwindow.h b/src/helpwindow.h new file mode 100644 index 0000000..7f04ddf --- /dev/null +++ b/src/helpwindow.h @@ -0,0 +1,23 @@ +#ifndef HELPWINDOW_H +#define HELPWINDOW_H + +#include + +namespace Ui { + class HelpWindow; +} + +class HelpWindow : public QWidget +{ + Q_OBJECT + +public: + explicit HelpWindow(QWidget *parent = 0); + ~HelpWindow(); + +private: + Ui::HelpWindow *ui; + void setHelpText(); +}; + +#endif // HELPWINDOW_H diff --git a/src/helpwindow.ui b/src/helpwindow.ui new file mode 100644 index 0000000..bfd7e2e --- /dev/null +++ b/src/helpwindow.ui @@ -0,0 +1,46 @@ + + + HelpWindow + + + + 0 + 0 + 530 + 415 + + + + Help + + + + + + + 0 + 0 + + + + background: transparent; + + + QFrame::NoFrame + + + QFrame::Plain + + + Qt::ScrollBarAlwaysOff + + + true + + + + + + + + diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index b2f34b2..dcc6f7a 100755 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -22,7 +22,6 @@ #include #include #include -#include #include #if defined(Q_WS_MAEMO_5) #include @@ -36,12 +35,17 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { + //this is a stacked window on Maemo 5 + #if defined(Q_WS_MAEMO_5) + setAttribute(Qt::WA_Maemo5StackedWindow); + #endif + ui->setupUi(this); refresh(); // enable auto rotation - setAutoRotaion(); + setAutoRotation(); //create a QGraphicsScene for the little chip icon scene = new QGraphicsScene(); @@ -52,20 +56,34 @@ MainWindow::MainWindow(QWidget *parent) : //refresh UI every 10 seconds refreshTimer->start( 10000 ); + //create helper process + helperProcess = new QProcess; + + //create a new, stackable help window + helpWindow = new HelpWindow( this ); + #if defined(Q_WS_MAEMO_5) + helpWindow->setAttribute(Qt::WA_Maemo5StackedWindow); + #endif + helpWindow->setWindowFlags( windowFlags() | Qt::Window ); + + //show errors about the sudo setup only once + showSudoError = true; + //connect signals and slots + connect(ui->actionHelp, SIGNAL(triggered()), this, SLOT(showHelp())); connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) ); connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) ); connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged())); - connect( ui->sr_btn, SIGNAL(clicked()), this, SLOT(setSmartReflex()) ); + connect(ui->sr_box, SIGNAL(clicked()), this, SLOT(setSmartReflex())); connect(refreshTimer, SIGNAL(timeout()), this, SLOT(refresh())); - } MainWindow::~MainWindow() { - delete ui; + delete helpWindow; delete refreshTimer; delete scene; + delete ui; } @@ -103,10 +121,7 @@ void MainWindow::adjustFreq() */ int MainWindow::callHelper(QString action, QString param) { - QProcess helper; QStringList arguments; - //run sudo in non-interactive mode - arguments.append( "-n" ); #if defined(Q_WS_MAEMO_5) //On Maemo 5 the helper script resides in /opt/usr/bin, which us usually not in $PATH @@ -118,9 +133,15 @@ int MainWindow::callHelper(QString action, QString param) arguments.append( action ); arguments.append( param ); - helper.execute( "sudo", arguments ); + helperProcess->start( "sudo", arguments, QIODevice::NotOpen ); + + if ( showSudoError && !helperProcess->waitForFinished( 200 )) { + //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 helper.exitCode(); + return helperProcess->exitCode(); } @@ -226,6 +247,9 @@ int MainWindow::getSmartReflexState() else return 0; #else + //disable UI checkbox + ui->sr_box->setDisabled( true ); + return 0; #endif } @@ -243,8 +267,8 @@ QString MainWindow::readSysFile(QString sys_file) //open the file if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) { - QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!")); - return ""; + QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!")); + return ""; } //read the file @@ -281,9 +305,9 @@ void MainWindow::refresh() //smart reflex button if ( getSmartReflexState() == 1 ) - ui->sr_btn->setText( tr( "Enabled" ) ); + ui->sr_box->setCheckState( Qt::Checked ); else - ui->sr_btn->setText( tr( "Disabled" ) ); + ui->sr_box->setCheckState( Qt::Unchecked ); //display frequency slider @@ -328,7 +352,7 @@ void MainWindow::orientationChanged() /** * Enables the auto-rotation feature of Maemo5 devices */ -void MainWindow::setAutoRotaion() +void MainWindow::setAutoRotation() { #if defined(Q_WS_MAEMO_5) setAttribute(Qt::WA_Maemo5AutoOrientation, true); @@ -357,6 +381,15 @@ void MainWindow::setSmartReflex() /** + * SLOT: display the help window + */ +void MainWindow::showHelp() +{ + helpWindow->show(); +} + + +/** * Returns true when the device is in portrait mode */ bool MainWindow::usePortrait() diff --git a/src/mainwindow.h b/src/mainwindow.h index 13eae27..9aa4763 100755 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -22,6 +22,9 @@ #include #include #include +#include + +#include "helpwindow.h" namespace Ui { class MainWindow; @@ -40,8 +43,9 @@ public slots: void adjustFreq(); void orientationChanged(); void refresh(); - void setAutoRotaion(); + void setAutoRotation(); void setSmartReflex(); + void showHelp(); private: @@ -55,11 +59,16 @@ private: int getScalingStep( int freq ); int getScalingSteps(); int getSmartReflexState(); + //! The helper process + QProcess *helperProcess; + //! The help window + HelpWindow *helpWindow; QString readSysFile( QString sys_file ); //! the timer for refreshing the UI QTimer *refreshTimer; //! the QGraphicsScene will contain the large chip icon displayed in the UI QGraphicsScene *scene; + bool showSudoError; bool usePortrait(); }; diff --git a/src/mainwindow.ui b/src/mainwindow.ui index fd4f6a6..eaf6ca2 100755 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -98,15 +98,15 @@ - + - + 0 0 - Disabled + Enable SR @@ -333,6 +333,7 @@ File + @@ -342,6 +343,11 @@ About + + + Help + + diff --git a/src/resources.qrc b/src/resources.qrc index 2a61cb1..d897943 100644 --- a/src/resources.qrc +++ b/src/resources.qrc @@ -3,4 +3,7 @@ data/128x128/chip.png data/256x256/chip.png + + help_en.html + diff --git a/src/src.pro b/src/src.pro index 555090f..39d3d75 100755 --- a/src/src.pro +++ b/src/src.pro @@ -11,11 +11,14 @@ TEMPLATE = app SOURCES += main.cpp\ - mainwindow.cpp + mainwindow.cpp \ + helpwindow.cpp -HEADERS += mainwindow.h +HEADERS += mainwindow.h \ + helpwindow.h -FORMS += mainwindow.ui +FORMS += mainwindow.ui \ + helpwindow.ui TRANSLATIONS = de.ts