Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --main.cpp--
- #include <QApplication>
- #include "mainwindow.h"
- #include "setpassword.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- return a.exec();
- }
- --mainwindow.cpp--
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #ifdef Q_WS_X11
- QString *OS=new QString("Linux");
- #endif
- #ifdef Q_WS_WIN
- QString *OS=new QString("Windows");
- #endif
- #ifdef Q_WS_MACX
- QString *OS=new QString("Mac");
- #endif
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- createActions();
- createTrayIcon () ;
- setIcon();
- trayIcon->show();
- iSeconds = 0 ;
- iMinutes = 0 ;
- iHours = 0 ;
- bSdEnabled = false ;
- iShowedSeconds = 0 ;
- iShowedMinutes = 0 ;
- iShowedHours = 0 ;
- timer = new QTimer (this) ;
- connect (timer, SIGNAL (timeout()), this, SLOT (updateTime())) ;
- timer->start (1000) ;
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- delete trayIcon;
- delete trayIconMenu;
- delete open;
- }
- void MainWindow::lineChanged()
- {
- ui->okButton->setEnabled(ui->sLineEdit->hasAcceptableInput() &&
- ui->mLineEdit->hasAcceptableInput() &&
- ui->hLineEdit->hasAcceptableInput());
- }
- void MainWindow::on_sLineEdit_editingFinished()
- {
- int iSecondsBef ;
- QString strSeconds ;
- iSecondsBef = GetIntVal(ui->sLineEdit->text().toStdString()) ;
- iShowedSeconds = iSecondsBef % 60 ;
- iSecondsBef = iSecondsBef / 60 ;
- strSeconds = IntToString(iShowedSeconds).c_str() ;
- ClearAndAdd (ui->sLineEdit, strSeconds) ;
- if (iSecondsBef > 0)
- {
- int iMinutesBef ;
- QString strMinutes ;
- iMinutesBef = iShowedMinutes + iSecondsBef;
- iShowedMinutes = iMinutesBef % 60 ;
- iMinutesBef = iMinutesBef / 60 ;
- strMinutes = IntToString (iShowedMinutes).c_str() ;
- ClearAndAdd(ui->mLineEdit, strMinutes);
- if(iMinutesBef > 0)
- {
- QString strHours ;
- iShowedHours = iShowedHours + iMinutesBef ;
- strHours = IntToString (iShowedHours).c_str() ;
- ClearAndAdd(ui->hLineEdit, strHours);
- }
- }
- }
- void MainWindow::on_mLineEdit_editingFinished()
- {
- int iMinutesBef ;
- QString strMinutes ;
- iMinutesBef = GetIntVal (ui->mLineEdit->text().toStdString()) ;
- iShowedMinutes = iMinutesBef % 60 ;
- iMinutesBef = iMinutesBef / 60 ;
- strMinutes = IntToString (iShowedMinutes).c_str() ;
- ClearAndAdd(ui->mLineEdit, strMinutes);
- if (iMinutesBef > 0)
- {
- QString strHours ;
- iShowedHours = iShowedHours + iMinutesBef ;
- strHours = IntToString(iShowedHours).c_str() ;
- ClearAndAdd(ui->hLineEdit, strHours);
- }
- }
- void MainWindow::on_hLineEdit_editingFinished()
- {
- QString strHours ;
- iShowedHours = GetIntVal (ui->hLineEdit->text().toStdString()) ;
- strHours = IntToString(iShowedHours).c_str() ;
- ClearAndAdd(ui->hLineEdit, strHours);
- }
- void MainWindow::on_abButton_clicked()
- {
- if (setPassword.isEmpty())
- {
- iHours = 0 ;
- iMinutes = 0 ;
- iSeconds = 0 ;
- bSdEnabled = false ;
- displayLcd() ;
- }
- else if (reqPassword())
- {
- iHours = 0 ;
- iMinutes = 0 ;
- iSeconds = 0 ;
- bSdEnabled = false ;
- displayLcd() ;
- }
- }
- void MainWindow::displayLcd ()
- {
- ui->lcdHours->display(iHours) ;
- ui->lcdMinutes->display(iMinutes) ;
- ui->lcdSeconds->display(iSeconds);
- }
- void MainWindow::on_exitButton_clicked()
- {
- toQuit() ;
- }
- void MainWindow::on_okButton_clicked()
- {
- if (setPassword.isEmpty())
- {
- iSeconds = iShowedSeconds ;
- iMinutes = iShowedMinutes ;
- iHours = iShowedHours ;
- bSdEnabled = true ;
- }
- else
- {
- if (reqPassword())
- {
- iSeconds = iShowedSeconds ;
- iMinutes = iShowedMinutes ;
- iHours = iShowedHours ;
- bSdEnabled = true ;
- }
- }
- }
- void MainWindow::updateTime()
- {
- if (bSdEnabled)
- {
- if (iHours == 0 && iMinutes == 0 && iSeconds == 0)
- {
- if (*OS == "Windows")
- system ("shutdown -s -f -t 0") ;
- // else if (*OS == "Linux")
- // execl("/usr/bin/sudo", "/sbin/shutdown", "-t", "now", (char*)NULL);
- else
- {
- QMessageBox::critical (this, tr("Error"),
- tr("OS not supoorted"), QMessageBox::Ok) ;
- bSdEnabled = false ;
- qApp->quit();
- }
- }
- else
- {
- if (iSeconds == 0)
- {
- if (iMinutes == 0)
- {
- iHours-- ;
- iMinutes = 59 ;
- iSeconds = 60 ;
- }
- else
- {
- iMinutes-- ;
- iSeconds = 60 ;
- }
- }
- iSeconds-- ;
- }
- displayLcd() ;
- }
- for (std::map<std::string, Note>::iterator iter = Notes.begin(); iter != Notes.end();)
- {
- if (iter->second.timeExpired(iHours, iMinutes, iSeconds))
- {
- if (iter->second.getTrayDisplay() == true)
- {
- trayIcon->showMessage(QString::fromUtf8(iter->second.getTitle().toStdString().c_str()),
- QString::fromUtf8(iter->second.getDetails().toStdString().c_str())) ;
- }
- if (iter->second.getWindowDisplay() == true)
- {
- if(isHidden())
- show() ;
- iter->second.displayNote() ;
- }
- Notes.erase(iter++) ;
- }
- else ++iter ;
- }
- }
- std::string MainWindow::IntToString(int IntValue)
- {
- char *MyBuff ;
- std::string strRetVal ;
- MyBuff = new char[100] ;
- memset (MyBuff, '\0', 100) ;
- itoa (IntValue, MyBuff, 10) ;
- strRetVal = MyBuff ;
- delete [] MyBuff ;
- return (strRetVal) ;
- }
- int MainWindow::GetIntVal(std::string StrConvert)
- {
- int intReturn ;
- intReturn = atoi (StrConvert.c_str()) ;
- return (intReturn) ;
- }
- void MainWindow::ClearAndAdd(QLineEdit* lineEdit, QString &text)
- {
- lineEdit->clear() ;
- lineEdit->insert(text);
- }
- void MainWindow::on_aboutQtAction_triggered()
- {
- qApp->aboutQt() ;
- }
- void MainWindow::on_exitAction_triggered()
- {
- toQuit() ;
- }
- void MainWindow::on_resetAction_triggered()
- {
- iShowedSeconds = 0 ;
- iShowedMinutes = 0 ;
- iShowedHours = 0 ;
- ui->sLineEdit->setText("0");
- ui->mLineEdit->setText("0");
- ui->hLineEdit->setText("0");
- }
- void MainWindow::on_aboutCptAction_triggered()
- {
- QMessageBox::about(this, tr("About Captain Shutdown"),
- tr("<h2>Captain Shutdown 2.5</h2>"
- "<p>Created by Radu Daniel Alexandru"
- "<p>danyhk94@gmail.com"
- "<p><a href = http://tinyprojectz.blogspot.com>My Blog</a>"
- "<p>Captain Shutdown is a free application "
- "that allows time scheduling of computer shutdown. "
- "Currently, it only works for Windows ."
- "<p><a href = http://www.softpedia.com/progClean/Captain-Shutdown-Clean-217944.html>Softpedia 100% Clean Award</a>")) ;
- }
- void MainWindow::on_setPAction_triggered()
- {
- SetPassword *setPas = new SetPassword(this) ;
- setPas->show() ;
- }
- void MainWindow::setPAction_correct()
- {
- QMessageBox::information (this, tr("Success"),
- tr("Password successfully set"), QMessageBox::Ok) ;
- ui->setPAction->setEnabled(false) ;
- ui->editPAction->setEnabled(true) ;
- ui->removePAction->setEnabled(true) ;
- }
- void MainWindow::setPAction_incorrect ()
- {
- setPassword.clear();
- }
- void MainWindow::setPAction_register(const QString &pass)
- {
- setPassword = pass ;
- }
- void MainWindow::closeEvent(QCloseEvent *event)
- {
- if(trayIcon->isVisible())
- {
- trayIcon->showMessage(tr("Hey ... I think it moved !!!"),
- tr("This application is still running. To quit please click the Exit button.")) ;
- hide() ;
- event->ignore();
- }
- }
- bool MainWindow::reqPassword()
- {
- bReqPassword = false ;
- AskPassword *askPas = new AskPassword (this, setPassword) ;
- askPas->exec();
- return bReqPassword ;
- }
- void MainWindow::slAskAccepted()
- {
- bReqPassword = true ;
- }
- void MainWindow::slAskRejcted()
- {
- bReqPassword = false ;
- }
- void MainWindow::on_editPAction_triggered()
- {
- if (reqPassword())
- {
- on_setPAction_triggered() ;
- }
- }
- void MainWindow::on_removePAction_triggered()
- {
- if (reqPassword())
- {
- setPassword.clear();
- }
- }
- bool MainWindow::getSdState()
- {
- return bSdEnabled ;
- }
- void MainWindow::on_addNoteAction_triggered()
- {
- if(setPassword.isEmpty())
- {
- AddNote *addNote = new AddNote (this, &Notes, bSdEnabled) ;
- addNote->show();
- }
- else
- if (reqPassword())
- {
- AddNote *addNote = new AddNote (this, &Notes, bSdEnabled) ;
- addNote->show();
- }
- }
- void MainWindow::toQuit()
- {
- if (setPassword.isEmpty())
- qApp->quit();
- else
- if (reqPassword())
- qApp->quit();
- }
- void MainWindow::createActions()
- {
- open = new QAction (tr("O&pen"), this) ;
- connect (open, SIGNAL (triggered()), this , SLOT (show())) ;
- }
- void MainWindow ::createTrayIcon()
- {
- trayIconMenu = new QMenu(this) ;
- trayIconMenu->addAction(open) ;
- trayIcon = new QSystemTrayIcon(this) ;
- trayIcon->setContextMenu(trayIconMenu) ;
- connect (trayIcon, SIGNAL (activated(QSystemTrayIcon::ActivationReason)),
- this, SLOT (trayIconClicked(QSystemTrayIcon::ActivationReason))) ;
- }
- void MainWindow::setIcon()
- {
- trayIcon->setIcon(QIcon("shutdown.png")) ;
- }
- void MainWindow::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
- {
- if (reason == QSystemTrayIcon::Trigger)
- {
- if(!bSdEnabled)
- trayIcon->showMessage(tr("Timer off"), tr("Shutdown timer hasn't been activated")) ;
- else
- {
- strTimer = IntToString (iHours) + " : " + IntToString(iMinutes) + " : " + IntToString (iSeconds) ;
- trayIcon->showMessage (tr("It's a bird, it's a plane, it's ..."), strTimer.c_str()) ;
- }
- }
- }
- void MainWindow::on_sLineEdit_focussed(bool focus)
- {
- if (focus)
- ui->sLineEdit->clear();
- else
- if (ui->sLineEdit->text().isEmpty())
- {
- ui->sLineEdit->setText(QString::fromUtf8("0")) ;
- iSeconds = 0 ;
- iShowedSeconds = 0 ;
- }
- }
- void MainWindow::on_mLineEdit_focussed(bool focus)
- {
- if (focus)
- ui->mLineEdit->clear();
- else
- if (ui->mLineEdit->text().isEmpty())
- {
- ui->mLineEdit->setText(QString::fromUtf8("0")) ;
- iMinutes = 0 ;
- iShowedMinutes = 0 ;
- }
- }
- void MainWindow::on_hLineEdit_focussed(bool focus)
- {
- if (focus)
- ui->hLineEdit->clear();
- else
- if (ui->hLineEdit->text().isEmpty())
- {
- ui->hLineEdit->setText(QString::fromUtf8("0")) ;
- iHours = 0 ;
- iShowedHours = 0 ;
- }
- }
- --mainwindow.h--
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include <QPushButton>
- #include <QLineEdit>
- #include <QMessageBox>
- #include <QMenu>
- #include <QCloseEvent>
- //#include <unistd.h>
- #include "setpassword.h"
- #include "askpassword.h"
- #include "addnote.h"
- #include "note.h"
- namespace Ui {
- class MainWindow;
- }
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit MainWindow(QWidget *parent = 0);
- ~MainWindow();
- bool getSdState () ;
- private:
- Ui::MainWindow *ui;
- protected:
- void closeEvent(QCloseEvent *event) ;
- private slots:
- void on_sLineEdit_focussed(bool focus) ;
- void on_mLineEdit_focussed(bool focus) ;
- void on_hLineEdit_focussed(bool focus) ;
- void on_sLineEdit_editingFinished() ;
- void on_mLineEdit_editingFinished() ;
- void on_hLineEdit_editingFinished() ;
- void lineChanged() ;
- void updateTime () ;
- void toQuit () ;
- void on_okButton_clicked() ;
- void on_abButton_clicked() ;
- void on_exitButton_clicked() ;
- void on_resetAction_triggered() ;
- void on_exitAction_triggered() ;
- void on_aboutCptAction_triggered() ;
- void on_aboutQtAction_triggered() ;
- void on_setPAction_triggered() ;
- void on_editPAction_triggered() ;
- void on_removePAction_triggered() ;
- void on_addNoteAction_triggered() ;
- void trayIconClicked (QSystemTrayIcon::ActivationReason) ;
- public slots:
- void slAskAccepted () ;
- void slAskRejcted () ;
- void setPAction_correct() ;
- void setPAction_incorrect () ;
- void setPAction_register(const QString &pass) ;
- private:
- std::string IntToString (int IntValue) ;
- int GetIntVal (std::string StrConvert) ;
- void ClearAndAdd (QLineEdit *lineEdit, QString &text) ;
- void displayLcd() ;
- bool reqPassword() ;
- void createActions() ;
- void createTrayIcon () ;
- void setIcon () ;
- void createMenus() ;
- std::map<std::string, Note> Notes ;
- QSystemTrayIcon *trayIcon ;
- QMenu *trayIconMenu ;
- QAction *open ;
- QString setPassword ;
- QString toCheckPass ;
- std::string strTimer ;
- QTimer *timer ;
- bool bReqPassword ;
- bool bSdEnabled ;
- int iSeconds ;
- int iMinutes ;
- int iHours ;
- int iShowedSeconds ;
- int iShowedMinutes ;
- int iShowedHours ;
- QMenu *optionsMenu ;
- QMenu *helpMenu ;
- QAction *resetAction ;
- QAction *aboutAction ;
- QAction *exitAction ;
- QSystemTrayIcon *mTray ;
- };
- #endif // MAINWINDOW_H
- --askpassword.cpp--
- #include "askpassword.h"
- #include "ui_askpassword.h"
- AskPassword::AskPassword(QWidget *parent, QString pass) :
- QDialog(parent),
- ui(new Ui::AskPassword),
- strPassword (pass)
- {
- ui->setupUi(this);
- ui->lineEdit->setEchoMode(QLineEdit::Password);
- connect (this, SIGNAL (accepted()), parent, SLOT(slAskAccepted())) ;
- connect (this, SIGNAL (rejected()), parent, SLOT (slAskRejected())) ;
- }
- AskPassword::~AskPassword()
- {
- delete ui;
- }
- void AskPassword::on_cancelButton_clicked()
- {
- ui->lineEdit->clear();
- reject() ;
- }
- void AskPassword::on_okButton_clicked()
- {
- if (ui->lineEdit->text() == strPassword)
- {
- accept() ;
- }
- else
- {
- QMessageBox::critical (this, tr("Error"),
- tr("Password doesn't match"), QMessageBox::Ok) ;
- ui->lineEdit->clear();
- }
- }
- void AskPassword::on_lineEdit_textChanged()
- {
- ui->okButton->setEnabled(!ui->lineEdit->text().isEmpty());
- }
- --askpassword.h--
- #ifndef ASKPASSWORD_H
- #define ASKPASSWORD_H
- #include <QtGui/QDialog>
- #include <QtGui>
- namespace Ui {
- class AskPassword;
- }
- class AskPassword : public QDialog
- {
- Q_OBJECT
- public:
- AskPassword(QWidget *parent = 0, QString pass = NULL);
- ~AskPassword();
- private:
- Ui::AskPassword *ui;
- QString strPassword ;
- private slots:
- void on_okButton_clicked() ;
- void on_cancelButton_clicked() ;
- void on_lineEdit_textChanged() ;
- };
- #endif // ASKPASSWORD_H
- --note.cpp--
- #include "note.h"
- Note::Note (QWidget *parent2) :
- parent(parent2)
- {
- }
- bool Note::checkAgainstTime(const int H, const int M, const int S)
- {
- return (H == iHours && M == iMinutes && S == iSeconds) ;
- }
- bool Note::elapseTime()
- {
- if (iSeconds == 0 && iMinutes == 0 && iHours == 0)
- {
- return true ;
- }
- else if (iSeconds == 0)
- {
- if (iMinutes == 0)
- {
- iHours-- ;
- iMinutes = 59 ;
- iSeconds = 60 ;
- }
- else
- {
- iMinutes-- ;
- iSeconds = 60 ;
- }
- }
- iSeconds-- ;
- return false ;
- }
- const int Note::getMode()
- {
- return iMode ;
- }
- void Note::setH(int H)
- {
- iHours = H ;
- }
- void Note::setM(int M)
- {
- iMinutes = M ;
- }
- void Note::setS(int S)
- {
- iSeconds = S ;
- }
- void Note::setMode(int Mo)
- {
- iMode = Mo ;
- }
- void Note::setTitle (QString T)
- {
- strTitle = T ;
- }
- void Note::setDetails (QString D)
- {
- strDetails = D ;
- }
- void Note::displayNote()
- {
- QMessageBox::information(parent ,strTitle,
- strDetails,
- QMessageBox::Ok) ;
- }
- bool Note::timeExpired(int H, int M, int S)
- {
- if (iMode == BEFORE_SHUT)
- {
- if (checkAgainstTime(H, M, S))
- {
- return true ;
- }
- }
- else
- {
- if (elapseTime())
- {
- return true ;
- }
- }
- return false ;
- }
- const int Note::getH()
- {
- return iHours ;
- }
- const int Note::getM()
- {
- return iMinutes ;
- }
- const int Note::getS()
- {
- return iSeconds ;
- }
- const QString Note::getDetails()
- {
- return strDetails ;
- }
- const QString Note::getTitle()
- {
- return strTitle ;
- }
- Note Note::operator =(Note &other)
- {
- this->setH(other.getH()) ;
- this->setM(other.getM()) ;
- this->setS(other.getS()) ;
- this->setMode(other.getMode()) ;
- this->setTitle(other.getTitle()) ;
- this->setDetails(other.getDetails()) ;
- this->setTrayDisplay(other.getTrayDisplay()) ;
- this->setWindowDisplay(other.getWindowDisplay()) ;
- return *this ;
- }
- const bool Note::getTrayDisplay()
- {
- return bTrayDisplay ;
- }
- const bool Note::getWindowDisplay()
- {
- return bWindowDisplay ;
- }
- void Note::setTrayDisplay(bool TDMo)
- {
- bTrayDisplay = TDMo ;
- }
- void Note::setWindowDisplay(bool WDMo)
- {
- bWindowDisplay = WDMo ;
- }
- --note.h--
- #ifndef NOTE_H
- #define NOTE_H
- #include <QString>
- #include <QtGui>
- #include <QMessageBox>
- #define BEFORE_SHUT 0
- #define AT_TIME 1
- class Note
- {
- public:
- Note(QWidget *parent2 = 0);
- Note operator= (Note& other) ;
- public:
- bool timeExpired (int H, int M, int S) ;
- void displayNote () ;
- const int getMode () ;
- const bool getTrayDisplay () ;
- const bool getWindowDisplay () ;
- const int getH () ;
- const int getM () ;
- const int getS () ;
- const QString getTitle () ;
- const QString getDetails () ;
- void setH (int H) ;
- void setM (int M) ;
- void setS (int S) ;
- void setMode (int Mo) ;
- void setTrayDisplay (bool TDMo) ;
- void setWindowDisplay (bool WDMo) ;
- void setTitle (QString T) ;
- void setDetails (QString D) ;
- private:
- bool checkAgainstTime (const int H, const int M, const int S) ;
- bool elapseTime () ;
- QWidget *parent ;
- int iHours ;
- int iMinutes ;
- int iSeconds ;
- int iMode ;
- bool bTrayDisplay ;
- bool bWindowDisplay ;
- QString strTitle ;
- QString strDetails ;
- };
- #endif // NOTE_H
- --setpassword.cpp--
- #include <QtGui>
- #include "setpassword.h"
- #include "ui_setpassword.h"
- extern MainWindow w ;
- SetPassword::SetPassword(QWidget *parent) :
- QDialog(parent),
- ui(new Ui::SetPassword)
- {
- ui->setupUi(this);
- ui->pass1->setEchoMode(QLineEdit::Password) ;
- ui->pass2->setEchoMode(QLineEdit::Password) ;
- connect (ui->pass1, SIGNAL (textEdited(const QString &)),
- parent, SLOT (setPAction_register(const QString &))) ;
- connect (this, SIGNAL (accepted()), parent, SLOT (setPAction_correct())) ;
- connect (this, SIGNAL (rejected()), parent, SLOT (setPAction_incorrect())) ;
- }
- SetPassword::~SetPassword()
- {
- delete ui;
- }
- void SetPassword::on_cancelButton_clicked()
- {
- ui->pass1->clear();
- ui->pass2->clear();
- reject() ;
- }
- void SetPassword::checkBut()
- {
- ui->okButton->setEnabled((!(ui->pass1->text().isEmpty()))
- && (!(ui->pass2->text().isEmpty())));
- }
- void SetPassword::on_pass1_textChanged()
- {
- checkBut() ;
- }
- void SetPassword::on_pass2_textChanged()
- {
- checkBut() ;
- }
- void SetPassword::on_okButton_clicked()
- {
- if (ui->pass1->text() == ui->pass2->text())
- {
- accept() ;
- }
- else
- {
- QMessageBox::critical (this, tr("Error"),
- tr("Passwords don't match"), QMessageBox::Ok) ;
- ui->pass1->clear();
- ui->pass2->clear();
- }
- }
- void SetPassword::closeEvent(QCloseEvent *event)
- {
- ui->pass1->clear();
- ui->pass2->clear();
- reject() ;
- }
- --setpassword.h--
- #ifndef SETPASSWORD_H
- #define SETPASSWORD_H
- #include <QDialog>
- #include "mainwindow.h"
- class MainWindow ;
- namespace Ui {
- class SetPassword;
- }
- class SetPassword : public QDialog
- {
- Q_OBJECT
- public:
- explicit SetPassword(QWidget *parent = 0);
- ~SetPassword();
- private:
- Ui::SetPassword *ui;
- protected:
- void closeEvent(QCloseEvent *event) ;
- private:
- void checkBut () ;
- private slots:
- void on_okButton_clicked() ;
- void on_cancelButton_clicked() ;
- void on_pass1_textChanged() ;
- void on_pass2_textChanged() ;
- };
- #endif // SETPASSWORD_H
- --addnote.cpp--
- #include "addnote.h"
- #include "ui_addnote.h"
- AddNote::AddNote(QWidget *parent, std::map<std::string, Note> *nNote,int sdState) :
- QDialog(parent),
- ui(new Ui::AddNote),
- vNote (nNote)
- {
- ui->setupUi(this);
- ui->beforeSdRadio->setEnabled(sdState) ;
- newNote.setH(0);
- newNote.setM(0);
- newNote.setS(0);
- newNote.setMode(AT_TIME) ;
- repopCombo () ;
- }
- AddNote::~AddNote()
- {
- vNote = NULL ;
- delete ui;
- }
- void AddNote::enableOK()
- {
- if (ui->hLineEdit->text().isEmpty()
- || ui->mLineEdit->text().isEmpty()
- || ui->sLineEdit->text().isEmpty()
- || ui->titleLineEdit->text().isEmpty()
- || (ui->trayCheckBox->isChecked() == false && ui->windowCheckBox->isChecked() == false))
- ui->okButton->setEnabled(false) ;
- else ui->okButton->setEnabled(true) ;
- }
- void AddNote::on_hLineEdit_editingFinished()
- {
- newNote.setH(atoi(ui->hLineEdit->text().toStdString().c_str()));
- enableOK() ;
- }
- void AddNote::on_mLineEdit_editingFinished()
- {
- if (GetIntVal(ui->mLineEdit->text().toStdString()) >= 60)
- {
- newNote.setH(newNote.getH() + GetIntVal(ui->mLineEdit->text().toStdString()) / 60) ;
- ui->hLineEdit->setText(QString::fromUtf8(IntToString(newNote.getH()).c_str())) ;
- }
- newNote.setM(atoi(ui->mLineEdit->text().toStdString().c_str()) % 60) ;
- ui->mLineEdit->setText(QString::fromUtf8(IntToString(newNote.getM()).c_str())) ;
- enableOK () ;
- }
- void AddNote::on_sLineEdit_editingFinished()
- {
- if (GetIntVal(ui->sLineEdit->text().toStdString()) >= 60)
- {
- newNote.setM(newNote.getM() + GetIntVal(ui->sLineEdit->text().toStdString()) / 60) ;
- ui->mLineEdit->setText(QString::fromUtf8(IntToString(newNote.getM()).c_str())) ;
- if (GetIntVal(ui->mLineEdit->text().toStdString()) >= 60)
- {
- newNote.setH(newNote.getH() + GetIntVal(ui->mLineEdit->text().toStdString()) / 60) ;
- ui->hLineEdit->setText(QString::fromUtf8(IntToString(newNote.getH()).c_str())) ;
- newNote.setM(atoi(ui->mLineEdit->text().toStdString().c_str()) % 60) ;
- ui->mLineEdit->setText(QString::fromUtf8(IntToString(newNote.getM()).c_str())) ;
- }
- }
- newNote.setS(atoi(ui->sLineEdit->text().toStdString().c_str()) % 60) ;
- ui->sLineEdit->setText(QString::fromUtf8(IntToString(newNote.getS()).c_str())) ;
- enableOK () ;
- }
- void AddNote::on_titleLineEdit_editingFinished()
- {
- if (ui->titleLineEdit->text() == "New" || nameInNotes(ui->titleLineEdit->text()))
- {
- QMessageBox::warning(this, tr("Warning"),
- tr("Please select other title"),
- QMessageBox::Ok) ;
- ui->titleLineEdit->clear();
- }
- else
- {
- newNote.setTitle(ui->titleLineEdit->text()) ;
- enableOK() ;
- }
- }
- void AddNote::on_detailsTextEdit_textChanged()
- {
- newNote.setDetails(ui->detailsTextEdit->toPlainText());
- }
- void AddNote::on_closeButton_clicked()
- {
- close() ;
- }
- void AddNote::on_okButton_clicked()
- {
- if (ui->comboBox->currentText() == "New")
- vNote->insert( make_pair(newNote.getTitle().toStdString(), newNote) ) ;
- else
- {
- vNote->find(ui->comboBox->currentText().toStdString())->second = newNote ;
- }
- close () ;
- }
- void AddNote::on_beforeSdRadio_clicked()
- {
- ui->fromSetRadio->setChecked(false) ;
- ui->beforeSdRadio->setChecked(true) ;
- newNote.setMode(BEFORE_SHUT);
- }
- void AddNote::on_fromSetRadio_clicked()
- {
- ui->fromSetRadio->setChecked(true) ;
- ui->beforeSdRadio->setChecked(false) ;
- newNote.setMode(AT_TIME);
- }
- void AddNote::on_comboBox_currentIndexChanged(const QString &textBox)
- {
- if(textBox == "New")
- {
- ui->titleLineEdit->setReadOnly (false) ;
- ui->titleLineEdit->clear();
- ui->detailsTextEdit->clear();
- ui->hLineEdit->setText(QString::fromUtf8(IntToString(newNote.getH()).c_str())) ;
- ui->mLineEdit->setText(QString::fromUtf8(IntToString(newNote.getM()).c_str())) ;
- ui->sLineEdit->setText(QString::fromUtf8(IntToString(newNote.getS()).c_str())) ;
- ui->deleteButton->setEnabled(false) ;
- ui->okButton->setText(tr("Add")) ;
- }
- else
- {
- newNote = vNote->find(ui->comboBox->currentText().toStdString())->second ;
- ui->titleLineEdit->setText(newNote.getTitle()) ;
- ui->detailsTextEdit->setText(newNote.getDetails()) ;
- ui->hLineEdit->setText(QString::fromUtf8(IntToString(newNote.getH()).c_str())) ;
- ui->mLineEdit->setText(QString::fromUtf8(IntToString(newNote.getM()).c_str())) ;
- ui->sLineEdit->setText(QString::fromUtf8(IntToString(newNote.getS()).c_str())) ;
- ui->beforeSdRadio->setChecked(newNote.getMode() == BEFORE_SHUT) ;
- ui->fromSetRadio->setChecked(newNote.getMode() == AT_TIME) ;
- ui->deleteButton->setEnabled(true) ;
- ui->okButton->setText(tr("Edit")) ;
- ui->titleLineEdit->setReadOnly(true) ;
- }
- }
- void AddNote::on_deleteButton_clicked()
- {
- if (vNote->find(ui->comboBox->currentText().toStdString()) != vNote->end())
- vNote->erase(vNote->find(ui->comboBox->currentText().toStdString())) ;
- repopCombo () ;
- }
- void AddNote::repopCombo()
- {
- disconnect (ui->comboBox, SIGNAL (currentIndexChanged(const QString)), 0, 0) ;
- ui->comboBox->clear() ;
- connect (ui->comboBox, SIGNAL (currentIndexChanged(const QString)), this, SLOT (on_comboBox_currentIndexChanged (const QString))) ;
- ui->comboBox->addItem(tr("New")) ;
- for( std::map<std::string, Note>::iterator iter = vNote->begin() ; iter!=vNote->end(); iter++)
- {
- ui->comboBox->addItem(iter->second.getTitle()) ;
- }
- }
- std::string AddNote::IntToString(int IntValue)
- {
- char *MyBuff ;
- std::string strRetVal ;
- MyBuff = new char[100] ;
- memset (MyBuff, '\0', 100) ;
- itoa (IntValue, MyBuff, 10) ;
- strRetVal = MyBuff ;
- delete [] MyBuff ;
- return (strRetVal) ;
- }
- int AddNote::GetIntVal(std::string StrConvert)
- {
- int intReturn ;
- intReturn = atoi (StrConvert.c_str()) ;
- return (intReturn) ;
- }
- void AddNote::on_sLineEdit_focussed(bool focus)
- {
- if (focus)
- ui->sLineEdit->clear();
- else
- if (ui->sLineEdit->text().isEmpty())
- {
- ui->sLineEdit->setText(QString::fromUtf8("0")) ;
- newNote.setS(0) ;
- }
- }
- void AddNote::on_mLineEdit_focussed(bool focus)
- {
- if(focus)
- ui->mLineEdit->clear() ;
- else
- if (ui->mLineEdit->text().isEmpty())
- {
- ui->mLineEdit->setText(QString::fromUtf8("0")) ;
- newNote.setM(0) ;
- }
- }
- void AddNote::on_hLineEdit_focussed(bool focus)
- {
- if(focus)
- ui->hLineEdit->clear() ;
- else
- if (ui->hLineEdit->text().isEmpty())
- {
- ui->hLineEdit->setText(QString::fromUtf8("0")) ;
- newNote.setH(0) ;
- }
- }
- void AddNote::on_trayCheckBox_stateChanged()
- {
- if (ui->trayCheckBox->isChecked()) newNote.setTrayDisplay(true) ;
- else newNote.setTrayDisplay(false) ;
- enableOK();
- }
- void AddNote::on_windowCheckBox_stateChanged()
- {
- if (ui->windowCheckBox->isChecked()) newNote.setWindowDisplay(true) ;
- else newNote.setWindowDisplay(false) ;
- enableOK();
- }
- bool AddNote::nameInNotes(QString qstrText)
- {
- for (std::map<std::string, Note>::iterator iter = vNote->begin(); iter != vNote->end(); iter++)
- {
- if (iter->second.getTitle() == qstrText) return true ;
- }
- return false ;
- }
- --addnote.h--
- #ifndef ADDNOTE_H
- #define ADDNOTE_H
- #include <QtGui/QDialog>
- #include <QLineEdit>
- #include <QtGui>
- #include <map>
- #include <string>
- #include "note.h"
- class Note ;
- namespace Ui {
- class AddNote;
- }
- class AddNote : public QDialog
- {
- Q_OBJECT
- public:
- explicit AddNote(QWidget *parent = 0, std::map<std::string, Note> *nNote = NULL,int sdState = 0) ;
- ~AddNote();
- private:
- Ui::AddNote *ui;
- Note newNote ;
- std::map<std::string, Note> *vNote;
- void enableOK () ;
- void repopCombo () ;
- bool nameInNotes (QString qstrText) ;
- std::string IntToString (int IntValue) ;
- int GetIntVal (std::string StrConvert) ;
- private slots:
- void on_trayCheckBox_stateChanged () ;
- void on_windowCheckBox_stateChanged () ;
- void on_titleLineEdit_editingFinished () ;
- void on_detailsTextEdit_textChanged () ;
- void on_hLineEdit_editingFinished () ;
- void on_mLineEdit_editingFinished () ;
- void on_sLineEdit_editingFinished () ;
- void on_sLineEdit_focussed(bool focus) ;
- void on_mLineEdit_focussed(bool focus) ;
- void on_hLineEdit_focussed(bool focus) ;
- void on_okButton_clicked () ;
- void on_closeButton_clicked() ;
- void on_fromSetRadio_clicked() ;
- void on_beforeSdRadio_clicked() ;
- void on_comboBox_currentIndexChanged (const QString &textBox) ;
- void on_deleteButton_clicked() ;
- };
- #endif // ADDNOTE_H
- --focuslineedit.cpp--
- #include "focuslineedit.h"
- FocusLineEdit::FocusLineEdit(QWidget *parent) :
- QLineEdit(parent)
- {
- }
- FocusLineEdit::~FocusLineEdit()
- {
- }
- void FocusLineEdit::focusInEvent(QFocusEvent *e)
- {
- QLineEdit::focusInEvent(e) ;
- emit(focussed(true)) ;
- }
- void FocusLineEdit::focusOutEvent(QFocusEvent *e)
- {
- QLineEdit::focusOutEvent(e) ;
- emit(focussed(false)) ;
- }
- --focuslineedit.h--
- #ifndef FOCUSLINEEDIT_H
- #define FOCUSLINEEDIT_H
- #include <QObject>
- #include <QLineEdit>
- class FocusLineEdit : public QLineEdit
- {
- Q_OBJECT
- public:
- explicit FocusLineEdit(QWidget *parent = 0);
- ~FocusLineEdit() ;
- signals:
- void focussed (bool hasFocus) ;
- public slots:
- protected:
- virtual void focusInEvent(QFocusEvent *e) ;
- virtual void focusOutEvent(QFocusEvent *e) ;
- };
- #endif // FOCUSLINEEDIT_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement