Advertisement
TwoOfDiamonds

CaptainShutdown

Jul 26th, 2012
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 31.57 KB | None | 0 0
  1. --main.cpp--
  2.  
  3. #include <QApplication>
  4. #include "mainwindow.h"
  5. #include "setpassword.h"
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     QApplication a(argc, argv);
  10.     MainWindow w;
  11.     w.show();
  12.    
  13.     return a.exec();
  14. }
  15.  
  16. --mainwindow.cpp--
  17.  
  18. #include "mainwindow.h"
  19.  
  20. #include "ui_mainwindow.h"
  21.  
  22. #ifdef Q_WS_X11
  23. QString *OS=new QString("Linux");
  24. #endif
  25. #ifdef Q_WS_WIN
  26. QString *OS=new QString("Windows");
  27. #endif
  28. #ifdef Q_WS_MACX
  29. QString *OS=new QString("Mac");
  30. #endif
  31.  
  32. MainWindow::MainWindow(QWidget *parent) :
  33.     QMainWindow(parent),
  34.     ui(new Ui::MainWindow)
  35. {    
  36.     ui->setupUi(this);
  37.  
  38.     createActions();
  39.     createTrayIcon () ;
  40.     setIcon();
  41.  
  42.     trayIcon->show();
  43.  
  44.     iSeconds = 0 ;
  45.     iMinutes = 0 ;
  46.     iHours = 0 ;
  47.  
  48.     bSdEnabled = false ;
  49.  
  50.     iShowedSeconds = 0 ;
  51.     iShowedMinutes = 0 ;
  52.     iShowedHours = 0 ;
  53.  
  54.     timer = new QTimer (this) ;
  55.     connect (timer, SIGNAL (timeout()), this, SLOT (updateTime())) ;
  56.     timer->start (1000) ;
  57. }
  58. MainWindow::~MainWindow()
  59. {  
  60.     delete ui;
  61.     delete trayIcon;
  62.     delete trayIconMenu;
  63.     delete open;
  64.  
  65. }
  66.  
  67. void MainWindow::lineChanged()
  68. {
  69.     ui->okButton->setEnabled(ui->sLineEdit->hasAcceptableInput() &&
  70.                          ui->mLineEdit->hasAcceptableInput() &&
  71.                          ui->hLineEdit->hasAcceptableInput());
  72. }
  73.  
  74. void MainWindow::on_sLineEdit_editingFinished()
  75. {
  76.     int iSecondsBef ;
  77.     QString strSeconds ;
  78.     iSecondsBef = GetIntVal(ui->sLineEdit->text().toStdString()) ;
  79.     iShowedSeconds = iSecondsBef % 60 ;
  80.     iSecondsBef = iSecondsBef / 60 ;
  81.     strSeconds = IntToString(iShowedSeconds).c_str() ;
  82.     ClearAndAdd (ui->sLineEdit, strSeconds) ;
  83.  
  84.     if (iSecondsBef > 0)
  85.     {
  86.         int iMinutesBef ;
  87.         QString strMinutes ;
  88.         iMinutesBef = iShowedMinutes + iSecondsBef;
  89.         iShowedMinutes = iMinutesBef % 60 ;
  90.         iMinutesBef = iMinutesBef / 60 ;
  91.         strMinutes = IntToString (iShowedMinutes).c_str() ;
  92.         ClearAndAdd(ui->mLineEdit, strMinutes);
  93.  
  94.         if(iMinutesBef > 0)
  95.         {
  96.             QString strHours ;
  97.             iShowedHours = iShowedHours + iMinutesBef ;
  98.             strHours = IntToString (iShowedHours).c_str() ;
  99.             ClearAndAdd(ui->hLineEdit, strHours);
  100.         }
  101.     }
  102.  
  103. }
  104.  
  105. void MainWindow::on_mLineEdit_editingFinished()
  106. {
  107.     int iMinutesBef ;
  108.     QString strMinutes ;
  109.     iMinutesBef = GetIntVal (ui->mLineEdit->text().toStdString()) ;
  110.     iShowedMinutes = iMinutesBef % 60 ;
  111.     iMinutesBef = iMinutesBef / 60 ;
  112.     strMinutes = IntToString (iShowedMinutes).c_str() ;
  113.     ClearAndAdd(ui->mLineEdit, strMinutes);
  114.  
  115.     if (iMinutesBef > 0)
  116.     {
  117.         QString strHours ;
  118.         iShowedHours = iShowedHours + iMinutesBef ;
  119.         strHours = IntToString(iShowedHours).c_str() ;
  120.         ClearAndAdd(ui->hLineEdit, strHours);
  121.     }
  122. }
  123.  
  124. void MainWindow::on_hLineEdit_editingFinished()
  125. {
  126.     QString strHours ;
  127.     iShowedHours = GetIntVal (ui->hLineEdit->text().toStdString()) ;
  128.     strHours = IntToString(iShowedHours).c_str() ;
  129.     ClearAndAdd(ui->hLineEdit, strHours);
  130. }
  131.  
  132.  
  133. void MainWindow::on_abButton_clicked()
  134. {
  135.     if (setPassword.isEmpty())
  136.     {
  137.         iHours = 0 ;
  138.         iMinutes = 0 ;
  139.         iSeconds = 0 ;
  140.  
  141.         bSdEnabled = false ;
  142.  
  143.         displayLcd() ;
  144.     }
  145.     else if (reqPassword())
  146.     {
  147.         iHours = 0 ;
  148.         iMinutes = 0 ;
  149.         iSeconds = 0 ;
  150.  
  151.         bSdEnabled = false ;
  152.  
  153.         displayLcd() ;
  154.  
  155.     }
  156. }
  157.  
  158. void MainWindow::displayLcd ()
  159. {
  160.     ui->lcdHours->display(iHours) ;
  161.     ui->lcdMinutes->display(iMinutes) ;
  162.     ui->lcdSeconds->display(iSeconds);
  163. }
  164.  
  165. void MainWindow::on_exitButton_clicked()
  166. {
  167.     toQuit() ;
  168. }
  169.  
  170. void MainWindow::on_okButton_clicked()
  171. {
  172.     if (setPassword.isEmpty())
  173.     {
  174.         iSeconds = iShowedSeconds ;
  175.         iMinutes = iShowedMinutes ;
  176.         iHours = iShowedHours ;
  177.  
  178.         bSdEnabled = true ;
  179.     }
  180.     else
  181.     {
  182.         if (reqPassword())
  183.         {
  184.             iSeconds = iShowedSeconds ;
  185.             iMinutes = iShowedMinutes ;
  186.             iHours = iShowedHours ;
  187.  
  188.             bSdEnabled = true ;
  189.         }
  190.     }
  191. }
  192.  
  193. void MainWindow::updateTime()
  194. {
  195.     if (bSdEnabled)
  196.     {
  197.         if (iHours == 0 && iMinutes == 0 && iSeconds == 0)
  198.         {
  199.             if (*OS == "Windows")
  200.                 system ("shutdown -s -f -t 0") ;
  201.  //           else if (*OS == "Linux")
  202.  //               execl("/usr/bin/sudo", "/sbin/shutdown", "-t", "now", (char*)NULL);
  203.             else
  204.                 {
  205.                     QMessageBox::critical (this, tr("Error"),
  206.                                            tr("OS not supoorted"), QMessageBox::Ok) ;
  207.                     bSdEnabled = false ;
  208.                     qApp->quit();
  209.                 }
  210.         }
  211.         else
  212.         {
  213.             if (iSeconds == 0)
  214.             {
  215.                 if (iMinutes == 0)
  216.                 {
  217.                     iHours-- ;
  218.                     iMinutes = 59 ;
  219.                     iSeconds = 60 ;
  220.                 }
  221.                 else
  222.                 {
  223.                     iMinutes-- ;
  224.                     iSeconds = 60 ;
  225.                 }
  226.             }
  227.  
  228.             iSeconds-- ;
  229.         }
  230.  
  231.         displayLcd() ;
  232.     }
  233.  
  234.     for (std::map<std::string, Note>::iterator iter = Notes.begin(); iter != Notes.end();)
  235.     {
  236.         if (iter->second.timeExpired(iHours, iMinutes, iSeconds))
  237.         {
  238.             if (iter->second.getTrayDisplay() == true)
  239.             {
  240.                 trayIcon->showMessage(QString::fromUtf8(iter->second.getTitle().toStdString().c_str()),
  241.                                       QString::fromUtf8(iter->second.getDetails().toStdString().c_str())) ;
  242.             }
  243.             if (iter->second.getWindowDisplay() == true)
  244.             {
  245.                 if(isHidden())
  246.                     show() ;
  247.                 iter->second.displayNote() ;
  248.             }
  249.             Notes.erase(iter++) ;
  250.         }
  251.         else ++iter ;
  252.     }
  253. }
  254.  
  255. std::string MainWindow::IntToString(int IntValue)
  256. {
  257.     char *MyBuff ;
  258.     std::string strRetVal ;
  259.  
  260.     MyBuff = new char[100] ;
  261.  
  262.     memset (MyBuff, '\0', 100) ;
  263.     itoa (IntValue, MyBuff, 10) ;
  264.  
  265.     strRetVal = MyBuff ;
  266.  
  267.     delete [] MyBuff ;
  268.  
  269.     return (strRetVal) ;
  270. }
  271.  
  272. int MainWindow::GetIntVal(std::string StrConvert)
  273. {
  274.     int intReturn ;
  275.  
  276.     intReturn = atoi (StrConvert.c_str()) ;
  277.  
  278.     return (intReturn) ;
  279. }
  280.  
  281. void MainWindow::ClearAndAdd(QLineEdit* lineEdit, QString &text)
  282. {
  283.     lineEdit->clear() ;
  284.     lineEdit->insert(text);
  285. }
  286.  
  287. void MainWindow::on_aboutQtAction_triggered()
  288. {
  289.     qApp->aboutQt() ;
  290. }
  291.  
  292. void MainWindow::on_exitAction_triggered()
  293. {
  294.     toQuit() ;
  295. }
  296.  
  297. void MainWindow::on_resetAction_triggered()
  298. {
  299.     iShowedSeconds = 0 ;
  300.     iShowedMinutes = 0 ;
  301.     iShowedHours = 0 ;
  302.  
  303.     ui->sLineEdit->setText("0");
  304.     ui->mLineEdit->setText("0");
  305.     ui->hLineEdit->setText("0");
  306. }
  307.  
  308. void MainWindow::on_aboutCptAction_triggered()
  309. {
  310.     QMessageBox::about(this, tr("About Captain Shutdown"),
  311.                        tr("<h2>Captain Shutdown 2.5</h2>"
  312.                        "<p>Created by Radu Daniel Alexandru"
  313.                        "<p>danyhk94@gmail.com"
  314.                         "<p><a href = http://tinyprojectz.blogspot.com>My Blog</a>"
  315.                        "<p>Captain Shutdown is a free application "
  316.                        "that allows time scheduling of computer shutdown. "
  317.                        "Currently, it only works for Windows ."
  318.                           "<p><a href = http://www.softpedia.com/progClean/Captain-Shutdown-Clean-217944.html>Softpedia 100% Clean Award</a>")) ;
  319. }
  320.  
  321. void MainWindow::on_setPAction_triggered()
  322. {
  323.     SetPassword *setPas = new SetPassword(this) ;
  324.  
  325.     setPas->show() ;
  326. }
  327.  
  328. void MainWindow::setPAction_correct()
  329. {
  330.  
  331.     QMessageBox::information (this, tr("Success"),
  332.                               tr("Password successfully set"), QMessageBox::Ok) ;
  333.     ui->setPAction->setEnabled(false) ;
  334.     ui->editPAction->setEnabled(true) ;
  335.     ui->removePAction->setEnabled(true) ;
  336. }
  337.  
  338. void MainWindow::setPAction_incorrect ()
  339. {
  340.     setPassword.clear();
  341. }
  342.  
  343. void MainWindow::setPAction_register(const QString &pass)
  344. {
  345.     setPassword = pass ;
  346. }
  347.  
  348. void MainWindow::closeEvent(QCloseEvent *event)
  349. {
  350.     if(trayIcon->isVisible())
  351.     {
  352.         trayIcon->showMessage(tr("Hey ... I think it moved !!!"),
  353.                               tr("This application is still running. To quit please click the Exit button.")) ;
  354.         hide() ;
  355.         event->ignore();
  356.     }
  357. }
  358.  
  359.  
  360.  
  361. bool MainWindow::reqPassword()
  362. {
  363.     bReqPassword = false ;
  364.  
  365.     AskPassword *askPas = new AskPassword (this, setPassword) ;
  366.  
  367.     askPas->exec();
  368.  
  369.     return bReqPassword ;
  370. }
  371.  
  372. void MainWindow::slAskAccepted()
  373. {
  374.     bReqPassword = true ;
  375. }
  376.  
  377. void MainWindow::slAskRejcted()
  378. {
  379.     bReqPassword = false ;
  380. }
  381.  
  382. void MainWindow::on_editPAction_triggered()
  383. {
  384.     if (reqPassword())
  385.     {
  386.         on_setPAction_triggered() ;
  387.     }
  388. }
  389.  
  390. void MainWindow::on_removePAction_triggered()
  391. {
  392.     if (reqPassword())
  393.     {
  394.         setPassword.clear();
  395.     }
  396. }
  397.  
  398. bool MainWindow::getSdState()
  399. {
  400.     return bSdEnabled ;
  401. }
  402.  
  403.  
  404. void MainWindow::on_addNoteAction_triggered()
  405. {
  406.     if(setPassword.isEmpty())
  407.     {
  408.         AddNote *addNote = new AddNote (this, &Notes, bSdEnabled) ;
  409.         addNote->show();
  410.     }
  411.     else
  412.         if (reqPassword())
  413.         {
  414.             AddNote *addNote = new AddNote (this, &Notes, bSdEnabled) ;
  415.             addNote->show();
  416.         }
  417. }
  418.  
  419. void MainWindow::toQuit()
  420. {
  421.     if (setPassword.isEmpty())
  422.         qApp->quit();
  423.     else
  424.         if (reqPassword())
  425.             qApp->quit();
  426. }
  427.  
  428. void MainWindow::createActions()
  429. {
  430.     open = new QAction (tr("O&pen"), this) ;
  431.     connect (open, SIGNAL (triggered()), this , SLOT (show())) ;
  432. }
  433.  
  434. void MainWindow ::createTrayIcon()
  435. {
  436.     trayIconMenu = new QMenu(this) ;
  437.  
  438.     trayIconMenu->addAction(open) ;
  439.  
  440.     trayIcon = new QSystemTrayIcon(this) ;
  441.     trayIcon->setContextMenu(trayIconMenu) ;
  442.  
  443.     connect (trayIcon, SIGNAL (activated(QSystemTrayIcon::ActivationReason)),
  444.              this, SLOT (trayIconClicked(QSystemTrayIcon::ActivationReason))) ;
  445. }
  446.  
  447. void MainWindow::setIcon()
  448. {
  449.     trayIcon->setIcon(QIcon("shutdown.png")) ;
  450. }
  451.  
  452. void MainWindow::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
  453. {
  454.     if (reason == QSystemTrayIcon::Trigger)
  455.     {
  456.         if(!bSdEnabled)
  457.             trayIcon->showMessage(tr("Timer off"), tr("Shutdown timer hasn't been activated")) ;
  458.         else
  459.         {
  460.             strTimer = IntToString (iHours) + " : " + IntToString(iMinutes) + " : " + IntToString (iSeconds) ;
  461.             trayIcon->showMessage (tr("It's a bird, it's a plane, it's ..."), strTimer.c_str()) ;
  462.         }
  463.  
  464.     }
  465. }
  466.  
  467. void MainWindow::on_sLineEdit_focussed(bool focus)
  468. {
  469.     if (focus)
  470.         ui->sLineEdit->clear();
  471.     else
  472.         if (ui->sLineEdit->text().isEmpty())
  473.         {
  474.             ui->sLineEdit->setText(QString::fromUtf8("0")) ;
  475.             iSeconds = 0 ;
  476.             iShowedSeconds = 0 ;
  477.         }
  478. }
  479.  
  480. void MainWindow::on_mLineEdit_focussed(bool focus)
  481. {
  482.     if (focus)
  483.         ui->mLineEdit->clear();
  484.     else
  485.         if (ui->mLineEdit->text().isEmpty())
  486.         {
  487.             ui->mLineEdit->setText(QString::fromUtf8("0")) ;
  488.             iMinutes = 0 ;
  489.             iShowedMinutes = 0 ;
  490.         }
  491. }
  492.  
  493. void MainWindow::on_hLineEdit_focussed(bool focus)
  494. {
  495.     if (focus)
  496.         ui->hLineEdit->clear();
  497.     else
  498.         if (ui->hLineEdit->text().isEmpty())
  499.         {
  500.             ui->hLineEdit->setText(QString::fromUtf8("0")) ;
  501.             iHours = 0 ;
  502.             iShowedHours = 0 ;
  503.         }
  504. }
  505.  
  506.  --mainwindow.h--
  507.  
  508. #ifndef MAINWINDOW_H
  509. #define MAINWINDOW_H
  510.  
  511. #include <QMainWindow>
  512. #include <QPushButton>
  513. #include <QLineEdit>
  514. #include <QMessageBox>
  515. #include <QMenu>
  516. #include <QCloseEvent>
  517. //#include <unistd.h>
  518.  
  519. #include "setpassword.h"
  520. #include "askpassword.h"
  521. #include "addnote.h"
  522. #include "note.h"
  523.  
  524. namespace Ui {
  525. class MainWindow;
  526. }
  527.  
  528. class MainWindow : public QMainWindow
  529. {
  530.     Q_OBJECT
  531.    
  532. public:
  533.     explicit MainWindow(QWidget *parent = 0);
  534.     ~MainWindow();
  535.  
  536.     bool getSdState () ;
  537.  
  538. private:
  539.     Ui::MainWindow *ui;
  540.  
  541. protected:
  542.     void closeEvent(QCloseEvent *event) ;
  543.  
  544. private slots:
  545.  
  546.     void on_sLineEdit_focussed(bool focus) ;
  547.     void on_mLineEdit_focussed(bool focus) ;
  548.     void on_hLineEdit_focussed(bool focus) ;
  549.  
  550.     void on_sLineEdit_editingFinished() ;
  551.     void on_mLineEdit_editingFinished() ;
  552.     void on_hLineEdit_editingFinished() ;
  553.  
  554.     void lineChanged() ;
  555.     void updateTime () ;
  556.     void toQuit () ;
  557.  
  558.     void on_okButton_clicked() ;
  559.     void on_abButton_clicked() ;
  560.     void on_exitButton_clicked() ;
  561.  
  562.     void on_resetAction_triggered() ;
  563.     void on_exitAction_triggered() ;
  564.  
  565.     void on_aboutCptAction_triggered() ;
  566.     void on_aboutQtAction_triggered() ;
  567.  
  568.     void on_setPAction_triggered() ;
  569.     void on_editPAction_triggered() ;
  570.     void on_removePAction_triggered() ;
  571.  
  572.     void on_addNoteAction_triggered() ;
  573.  
  574.     void trayIconClicked (QSystemTrayIcon::ActivationReason) ;
  575.  
  576. public slots:
  577.     void slAskAccepted () ;
  578.     void slAskRejcted () ;
  579.  
  580.     void setPAction_correct() ;
  581.     void setPAction_incorrect () ;
  582.     void setPAction_register(const QString &pass) ;
  583.  
  584. private:
  585.  
  586.     std::string IntToString (int IntValue) ;
  587.     int GetIntVal (std::string StrConvert) ;
  588.     void ClearAndAdd (QLineEdit *lineEdit, QString &text) ;
  589.     void displayLcd() ;
  590.  
  591.     bool reqPassword() ;
  592.  
  593.     void createActions() ;
  594.     void createTrayIcon () ;
  595.     void setIcon () ;
  596.     void createMenus() ;
  597.  
  598.     std::map<std::string, Note> Notes ;
  599.  
  600.     QSystemTrayIcon *trayIcon ;
  601.     QMenu *trayIconMenu ;
  602.  
  603.     QAction *open ;
  604.  
  605.     QString setPassword ;
  606.     QString toCheckPass ;
  607.  
  608.     std::string strTimer ;
  609.  
  610.     QTimer *timer ;
  611.  
  612.     bool bReqPassword ;
  613.     bool bSdEnabled ;
  614.  
  615.     int iSeconds ;
  616.     int iMinutes ;
  617.     int iHours ;
  618.     int iShowedSeconds ;
  619.     int iShowedMinutes ;
  620.     int iShowedHours ;
  621.  
  622.     QMenu *optionsMenu ;
  623.     QMenu *helpMenu ;
  624.  
  625.     QAction *resetAction ;
  626.     QAction *aboutAction ;
  627.     QAction *exitAction ;
  628.  
  629.     QSystemTrayIcon *mTray ;
  630. };
  631.  
  632. #endif // MAINWINDOW_H
  633.  
  634.  --askpassword.cpp--
  635.  
  636. #include "askpassword.h"
  637. #include "ui_askpassword.h"
  638.  
  639. AskPassword::AskPassword(QWidget *parent, QString pass) :
  640.     QDialog(parent),
  641.     ui(new Ui::AskPassword),
  642.     strPassword (pass)
  643. {
  644.     ui->setupUi(this);
  645.  
  646.     ui->lineEdit->setEchoMode(QLineEdit::Password);
  647.  
  648.     connect (this, SIGNAL (accepted()), parent, SLOT(slAskAccepted())) ;
  649.     connect (this, SIGNAL (rejected()), parent, SLOT (slAskRejected())) ;
  650. }
  651.  
  652. AskPassword::~AskPassword()
  653. {
  654.     delete ui;
  655. }
  656.  
  657. void AskPassword::on_cancelButton_clicked()
  658. {
  659.     ui->lineEdit->clear();
  660.  
  661.     reject() ;
  662. }
  663.  
  664. void AskPassword::on_okButton_clicked()
  665. {
  666.     if (ui->lineEdit->text() == strPassword)
  667.     {
  668.         accept() ;
  669.     }
  670.     else
  671.     {
  672.         QMessageBox::critical (this, tr("Error"),
  673.                                tr("Password doesn't match"), QMessageBox::Ok) ;
  674.         ui->lineEdit->clear();
  675.     }
  676. }
  677.  
  678. void AskPassword::on_lineEdit_textChanged()
  679. {
  680.     ui->okButton->setEnabled(!ui->lineEdit->text().isEmpty());
  681. }
  682.  
  683.  --askpassword.h--
  684.  
  685. #ifndef ASKPASSWORD_H
  686. #define ASKPASSWORD_H
  687.  
  688. #include <QtGui/QDialog>
  689. #include <QtGui>
  690.  
  691. namespace Ui {
  692. class AskPassword;
  693. }
  694.  
  695. class AskPassword : public QDialog
  696. {
  697.     Q_OBJECT
  698.    
  699. public:
  700.     AskPassword(QWidget *parent = 0, QString pass = NULL);
  701.     ~AskPassword();
  702.    
  703. private:
  704.     Ui::AskPassword *ui;
  705.     QString strPassword ;
  706.  
  707. private slots:
  708.  
  709.     void on_okButton_clicked() ;
  710.     void on_cancelButton_clicked() ;
  711.  
  712.     void on_lineEdit_textChanged() ;
  713. };
  714.  
  715. #endif // ASKPASSWORD_H
  716.  
  717.  --note.cpp--
  718.  
  719. #include "note.h"
  720.  
  721. Note::Note (QWidget *parent2) :
  722.     parent(parent2)
  723. {
  724. }
  725.  
  726. bool Note::checkAgainstTime(const int H, const int M, const int S)
  727. {
  728.     return (H == iHours && M == iMinutes && S == iSeconds) ;
  729. }
  730.  
  731. bool Note::elapseTime()
  732. {
  733.     if (iSeconds == 0 && iMinutes == 0 && iHours == 0)
  734.     {
  735.         return true ;
  736.     }
  737.     else if (iSeconds == 0)
  738.     {
  739.         if (iMinutes == 0)
  740.         {
  741.             iHours-- ;
  742.             iMinutes = 59 ;
  743.             iSeconds = 60 ;
  744.         }
  745.         else
  746.         {
  747.             iMinutes-- ;
  748.             iSeconds = 60 ;
  749.         }
  750.     }
  751.  
  752.     iSeconds-- ;
  753.  
  754.     return false ;
  755. }
  756.  
  757. const int Note::getMode()
  758. {
  759.     return iMode ;
  760. }
  761.  
  762. void Note::setH(int H)
  763. {
  764.     iHours = H ;
  765. }
  766.  
  767. void Note::setM(int M)
  768. {
  769.     iMinutes = M ;
  770. }
  771.  
  772. void Note::setS(int S)
  773. {
  774.     iSeconds = S ;
  775. }
  776.  
  777. void Note::setMode(int Mo)
  778. {
  779.     iMode = Mo ;
  780. }
  781.  
  782. void Note::setTitle (QString T)
  783. {
  784.     strTitle = T ;
  785. }
  786.  
  787. void Note::setDetails (QString D)
  788. {
  789.     strDetails = D ;
  790. }
  791.  
  792. void Note::displayNote()
  793. {
  794.     QMessageBox::information(parent ,strTitle,
  795.                              strDetails,
  796.                                 QMessageBox::Ok) ;
  797. }
  798.  
  799. bool Note::timeExpired(int H, int M, int S)
  800. {
  801.     if (iMode == BEFORE_SHUT)
  802.     {
  803.         if (checkAgainstTime(H, M, S))
  804.         {
  805.             return true ;
  806.         }
  807.     }
  808.     else
  809.     {
  810.         if (elapseTime())
  811.         {
  812.             return true ;
  813.         }
  814.     }
  815.  
  816.     return false ;
  817. }
  818.  
  819. const int Note::getH()
  820. {
  821.     return iHours ;
  822. }
  823.  
  824. const int Note::getM()
  825. {
  826.     return iMinutes ;
  827. }
  828.  
  829. const int Note::getS()
  830. {
  831.     return iSeconds ;
  832. }
  833.  
  834. const QString Note::getDetails()
  835. {
  836.     return strDetails ;
  837. }
  838.  
  839. const QString Note::getTitle()
  840. {
  841.     return strTitle ;
  842. }
  843.  
  844. Note Note::operator =(Note &other)
  845. {
  846.     this->setH(other.getH()) ;
  847.     this->setM(other.getM()) ;
  848.     this->setS(other.getS()) ;
  849.     this->setMode(other.getMode()) ;
  850.     this->setTitle(other.getTitle()) ;
  851.     this->setDetails(other.getDetails()) ;
  852.     this->setTrayDisplay(other.getTrayDisplay()) ;
  853.     this->setWindowDisplay(other.getWindowDisplay()) ;
  854.  
  855.     return *this ;
  856. }
  857.  
  858. const bool Note::getTrayDisplay()
  859. {
  860.     return bTrayDisplay ;
  861. }
  862.  
  863. const bool Note::getWindowDisplay()
  864. {
  865.     return bWindowDisplay ;
  866. }
  867.  
  868. void Note::setTrayDisplay(bool TDMo)
  869. {
  870.     bTrayDisplay = TDMo ;
  871. }
  872.  
  873. void Note::setWindowDisplay(bool WDMo)
  874. {
  875.     bWindowDisplay = WDMo ;
  876. }
  877.  
  878.  --note.h--
  879.  
  880. #ifndef NOTE_H
  881. #define NOTE_H
  882.  
  883. #include <QString>
  884. #include <QtGui>
  885. #include <QMessageBox>
  886.  
  887. #define BEFORE_SHUT 0
  888. #define AT_TIME 1
  889.  
  890. class Note
  891. {
  892. public:
  893.     Note(QWidget *parent2 = 0);
  894.  
  895.     Note operator= (Note& other) ;
  896.  
  897. public:
  898.     bool timeExpired (int H, int M, int S) ;
  899.     void displayNote () ;
  900.  
  901.     const int getMode () ;
  902.     const bool getTrayDisplay () ;
  903.     const bool getWindowDisplay () ;
  904.     const int getH () ;
  905.     const int getM () ;
  906.     const int getS () ;
  907.     const QString getTitle () ;
  908.     const QString getDetails () ;
  909.  
  910.     void setH (int H) ;
  911.     void setM (int M) ;
  912.     void setS (int S) ;
  913.     void setMode (int Mo) ;
  914.     void setTrayDisplay (bool TDMo) ;
  915.     void setWindowDisplay (bool WDMo) ;
  916.     void setTitle (QString T) ;
  917.     void setDetails (QString D) ;
  918.  
  919. private:
  920.     bool checkAgainstTime (const int H, const int M, const int S) ;
  921.     bool elapseTime () ;
  922.  
  923.     QWidget *parent ;
  924.  
  925.     int iHours ;
  926.     int iMinutes ;
  927.     int iSeconds ;
  928.  
  929.     int iMode ;
  930.     bool bTrayDisplay ;
  931.     bool bWindowDisplay ;
  932.  
  933.     QString strTitle ;
  934.     QString strDetails ;
  935.  
  936. };
  937.  
  938. #endif // NOTE_H
  939.  
  940.  --setpassword.cpp--
  941.  
  942. #include <QtGui>
  943.  
  944. #include "setpassword.h"
  945. #include "ui_setpassword.h"
  946.  
  947. extern MainWindow w ;
  948.  
  949. SetPassword::SetPassword(QWidget *parent) :
  950.     QDialog(parent),
  951.     ui(new Ui::SetPassword)
  952. {
  953.     ui->setupUi(this);
  954.  
  955.     ui->pass1->setEchoMode(QLineEdit::Password) ;
  956.     ui->pass2->setEchoMode(QLineEdit::Password) ;
  957.  
  958.     connect (ui->pass1, SIGNAL (textEdited(const QString &)),
  959.              parent, SLOT (setPAction_register(const QString &))) ;
  960.  
  961.     connect (this, SIGNAL (accepted()), parent, SLOT (setPAction_correct())) ;
  962.     connect (this, SIGNAL (rejected()), parent, SLOT (setPAction_incorrect())) ;
  963. }
  964.  
  965. SetPassword::~SetPassword()
  966. {
  967.     delete ui;
  968. }
  969.  
  970. void SetPassword::on_cancelButton_clicked()
  971. {
  972.     ui->pass1->clear();
  973.     ui->pass2->clear();
  974.  
  975.     reject() ;
  976. }
  977.  
  978. void SetPassword::checkBut()
  979. {
  980.     ui->okButton->setEnabled((!(ui->pass1->text().isEmpty()))
  981.                                  && (!(ui->pass2->text().isEmpty())));
  982. }
  983.  
  984. void SetPassword::on_pass1_textChanged()
  985. {
  986.     checkBut() ;
  987. }
  988.  
  989. void SetPassword::on_pass2_textChanged()
  990. {
  991.     checkBut() ;
  992. }
  993.  
  994. void SetPassword::on_okButton_clicked()
  995. {
  996.     if (ui->pass1->text() == ui->pass2->text())
  997.     {
  998.         accept() ;
  999.     }
  1000.  
  1001.     else
  1002.     {
  1003.         QMessageBox::critical (this, tr("Error"),
  1004.                                tr("Passwords don't match"), QMessageBox::Ok) ;
  1005.         ui->pass1->clear();
  1006.         ui->pass2->clear();
  1007.     }
  1008. }
  1009.  
  1010. void SetPassword::closeEvent(QCloseEvent *event)
  1011. {
  1012.     ui->pass1->clear();
  1013.     ui->pass2->clear();
  1014.  
  1015.     reject() ;
  1016. }
  1017.  
  1018.  --setpassword.h--
  1019.  
  1020. #ifndef SETPASSWORD_H
  1021. #define SETPASSWORD_H
  1022.  
  1023. #include <QDialog>
  1024.  
  1025. #include "mainwindow.h"
  1026.  
  1027. class MainWindow ;
  1028.  
  1029. namespace Ui {
  1030. class SetPassword;
  1031. }
  1032.  
  1033. class SetPassword : public QDialog
  1034. {
  1035.     Q_OBJECT
  1036.    
  1037. public:
  1038.     explicit SetPassword(QWidget *parent = 0);
  1039.     ~SetPassword();
  1040.    
  1041. private:
  1042.     Ui::SetPassword *ui;
  1043.  
  1044. protected:
  1045.     void closeEvent(QCloseEvent *event) ;
  1046.  
  1047. private:
  1048.     void checkBut () ;
  1049.  
  1050. private slots:
  1051.     void on_okButton_clicked() ;
  1052.     void on_cancelButton_clicked() ;
  1053.  
  1054.     void on_pass1_textChanged() ;
  1055.     void on_pass2_textChanged() ;
  1056. };
  1057.  
  1058. #endif // SETPASSWORD_H
  1059.  
  1060.  --addnote.cpp--
  1061.  
  1062. #include "addnote.h"
  1063. #include "ui_addnote.h"
  1064.  
  1065. AddNote::AddNote(QWidget *parent, std::map<std::string, Note> *nNote,int sdState) :
  1066.     QDialog(parent),
  1067.     ui(new Ui::AddNote),
  1068.     vNote (nNote)
  1069. {
  1070.     ui->setupUi(this);
  1071.  
  1072.     ui->beforeSdRadio->setEnabled(sdState) ;
  1073.  
  1074.     newNote.setH(0);
  1075.     newNote.setM(0);
  1076.     newNote.setS(0);
  1077.  
  1078.     newNote.setMode(AT_TIME) ;
  1079.  
  1080.     repopCombo () ;
  1081.  
  1082. }
  1083.  
  1084. AddNote::~AddNote()
  1085. {
  1086.     vNote = NULL ;
  1087.     delete ui;
  1088. }
  1089.  
  1090. void AddNote::enableOK()
  1091. {
  1092.     if (ui->hLineEdit->text().isEmpty()
  1093.             || ui->mLineEdit->text().isEmpty()
  1094.             || ui->sLineEdit->text().isEmpty()
  1095.             || ui->titleLineEdit->text().isEmpty()
  1096.             || (ui->trayCheckBox->isChecked() == false && ui->windowCheckBox->isChecked() == false))
  1097.         ui->okButton->setEnabled(false) ;
  1098.  
  1099.     else ui->okButton->setEnabled(true) ;
  1100. }
  1101.  
  1102. void AddNote::on_hLineEdit_editingFinished()
  1103. {
  1104.     newNote.setH(atoi(ui->hLineEdit->text().toStdString().c_str()));
  1105.     enableOK() ;
  1106. }
  1107.  
  1108. void AddNote::on_mLineEdit_editingFinished()
  1109. {
  1110.     if (GetIntVal(ui->mLineEdit->text().toStdString()) >= 60)
  1111.     {
  1112.         newNote.setH(newNote.getH() + GetIntVal(ui->mLineEdit->text().toStdString()) / 60) ;
  1113.         ui->hLineEdit->setText(QString::fromUtf8(IntToString(newNote.getH()).c_str())) ;
  1114.     }
  1115.  
  1116.     newNote.setM(atoi(ui->mLineEdit->text().toStdString().c_str()) % 60) ;
  1117.     ui->mLineEdit->setText(QString::fromUtf8(IntToString(newNote.getM()).c_str())) ;
  1118.  
  1119.     enableOK () ;
  1120. }
  1121. void AddNote::on_sLineEdit_editingFinished()
  1122. {
  1123.     if (GetIntVal(ui->sLineEdit->text().toStdString()) >= 60)
  1124.     {
  1125.         newNote.setM(newNote.getM() + GetIntVal(ui->sLineEdit->text().toStdString()) / 60) ;
  1126.         ui->mLineEdit->setText(QString::fromUtf8(IntToString(newNote.getM()).c_str())) ;
  1127.  
  1128.         if (GetIntVal(ui->mLineEdit->text().toStdString()) >= 60)
  1129.         {
  1130.             newNote.setH(newNote.getH() + GetIntVal(ui->mLineEdit->text().toStdString()) / 60) ;
  1131.             ui->hLineEdit->setText(QString::fromUtf8(IntToString(newNote.getH()).c_str())) ;
  1132.  
  1133.             newNote.setM(atoi(ui->mLineEdit->text().toStdString().c_str()) % 60) ;
  1134.             ui->mLineEdit->setText(QString::fromUtf8(IntToString(newNote.getM()).c_str())) ;
  1135.         }
  1136.     }
  1137.  
  1138.     newNote.setS(atoi(ui->sLineEdit->text().toStdString().c_str()) % 60) ;
  1139.     ui->sLineEdit->setText(QString::fromUtf8(IntToString(newNote.getS()).c_str())) ;
  1140.  
  1141.     enableOK () ;
  1142. }
  1143. void AddNote::on_titleLineEdit_editingFinished()
  1144. {
  1145.     if (ui->titleLineEdit->text() == "New" || nameInNotes(ui->titleLineEdit->text()))
  1146.     {
  1147.         QMessageBox::warning(this, tr("Warning"),
  1148.                             tr("Please select other title"),
  1149.                             QMessageBox::Ok) ;
  1150.         ui->titleLineEdit->clear();
  1151.     }
  1152.     else
  1153.     {
  1154.         newNote.setTitle(ui->titleLineEdit->text()) ;
  1155.         enableOK() ;
  1156.     }
  1157. }
  1158. void AddNote::on_detailsTextEdit_textChanged()
  1159. {
  1160.     newNote.setDetails(ui->detailsTextEdit->toPlainText());
  1161. }
  1162.  
  1163. void AddNote::on_closeButton_clicked()
  1164. {
  1165.     close() ;
  1166. }
  1167.  
  1168. void AddNote::on_okButton_clicked()
  1169. {
  1170.     if (ui->comboBox->currentText() == "New")
  1171.         vNote->insert( make_pair(newNote.getTitle().toStdString(), newNote) ) ;
  1172.     else
  1173.     {
  1174.         vNote->find(ui->comboBox->currentText().toStdString())->second = newNote ;
  1175.     }
  1176.     close () ;
  1177. }
  1178.  
  1179. void AddNote::on_beforeSdRadio_clicked()
  1180. {
  1181.     ui->fromSetRadio->setChecked(false) ;
  1182.     ui->beforeSdRadio->setChecked(true) ;
  1183.  
  1184.     newNote.setMode(BEFORE_SHUT);
  1185. }
  1186.  
  1187. void AddNote::on_fromSetRadio_clicked()
  1188. {
  1189.     ui->fromSetRadio->setChecked(true) ;
  1190.     ui->beforeSdRadio->setChecked(false) ;
  1191.  
  1192.     newNote.setMode(AT_TIME);
  1193. }
  1194.  
  1195. void AddNote::on_comboBox_currentIndexChanged(const QString &textBox)
  1196. {
  1197.     if(textBox == "New")
  1198.     {
  1199.         ui->titleLineEdit->setReadOnly (false) ;
  1200.         ui->titleLineEdit->clear();
  1201.         ui->detailsTextEdit->clear();
  1202.         ui->hLineEdit->setText(QString::fromUtf8(IntToString(newNote.getH()).c_str())) ;
  1203.         ui->mLineEdit->setText(QString::fromUtf8(IntToString(newNote.getM()).c_str())) ;
  1204.         ui->sLineEdit->setText(QString::fromUtf8(IntToString(newNote.getS()).c_str())) ;
  1205.         ui->deleteButton->setEnabled(false) ;
  1206.         ui->okButton->setText(tr("Add")) ;
  1207.     }
  1208.  
  1209.     else
  1210.     {
  1211.         newNote = vNote->find(ui->comboBox->currentText().toStdString())->second ;
  1212.  
  1213.         ui->titleLineEdit->setText(newNote.getTitle()) ;
  1214.         ui->detailsTextEdit->setText(newNote.getDetails()) ;
  1215.         ui->hLineEdit->setText(QString::fromUtf8(IntToString(newNote.getH()).c_str())) ;
  1216.         ui->mLineEdit->setText(QString::fromUtf8(IntToString(newNote.getM()).c_str())) ;
  1217.         ui->sLineEdit->setText(QString::fromUtf8(IntToString(newNote.getS()).c_str())) ;
  1218.         ui->beforeSdRadio->setChecked(newNote.getMode() == BEFORE_SHUT) ;
  1219.         ui->fromSetRadio->setChecked(newNote.getMode() == AT_TIME) ;
  1220.  
  1221.         ui->deleteButton->setEnabled(true) ;
  1222.         ui->okButton->setText(tr("Edit")) ;
  1223.         ui->titleLineEdit->setReadOnly(true) ;
  1224.     }
  1225. }
  1226.  
  1227. void AddNote::on_deleteButton_clicked()
  1228. {
  1229.     if (vNote->find(ui->comboBox->currentText().toStdString()) != vNote->end())
  1230.         vNote->erase(vNote->find(ui->comboBox->currentText().toStdString())) ;
  1231.  
  1232.     repopCombo () ;
  1233. }
  1234.  
  1235. void AddNote::repopCombo()
  1236. {
  1237.     disconnect (ui->comboBox, SIGNAL (currentIndexChanged(const QString)), 0, 0) ;
  1238.  
  1239.     ui->comboBox->clear() ;
  1240.  
  1241.     connect (ui->comboBox, SIGNAL (currentIndexChanged(const QString)), this, SLOT (on_comboBox_currentIndexChanged (const QString))) ;
  1242.  
  1243.     ui->comboBox->addItem(tr("New")) ;
  1244.  
  1245.     for( std::map<std::string, Note>::iterator iter = vNote->begin() ; iter!=vNote->end(); iter++)
  1246.     {
  1247.         ui->comboBox->addItem(iter->second.getTitle()) ;
  1248.     }
  1249. }
  1250.  
  1251.  
  1252. std::string AddNote::IntToString(int IntValue)
  1253. {
  1254.     char *MyBuff ;
  1255.     std::string strRetVal ;
  1256.  
  1257.     MyBuff = new char[100] ;
  1258.  
  1259.     memset (MyBuff, '\0', 100) ;
  1260.     itoa (IntValue, MyBuff, 10) ;
  1261.  
  1262.     strRetVal = MyBuff ;
  1263.  
  1264.     delete [] MyBuff ;
  1265.  
  1266.     return (strRetVal) ;
  1267. }
  1268.  
  1269. int AddNote::GetIntVal(std::string StrConvert)
  1270. {
  1271.     int intReturn ;
  1272.  
  1273.     intReturn = atoi (StrConvert.c_str()) ;
  1274.  
  1275.     return (intReturn) ;
  1276. }
  1277.  
  1278. void AddNote::on_sLineEdit_focussed(bool focus)
  1279. {
  1280.     if (focus)
  1281.         ui->sLineEdit->clear();
  1282.     else
  1283.         if (ui->sLineEdit->text().isEmpty())
  1284.         {
  1285.             ui->sLineEdit->setText(QString::fromUtf8("0")) ;
  1286.             newNote.setS(0) ;
  1287.         }
  1288. }
  1289.  
  1290. void AddNote::on_mLineEdit_focussed(bool focus)
  1291. {
  1292.     if(focus)
  1293.         ui->mLineEdit->clear() ;
  1294.     else
  1295.         if (ui->mLineEdit->text().isEmpty())
  1296.         {
  1297.             ui->mLineEdit->setText(QString::fromUtf8("0")) ;
  1298.             newNote.setM(0) ;
  1299.         }
  1300. }
  1301.  
  1302. void AddNote::on_hLineEdit_focussed(bool focus)
  1303. {
  1304.     if(focus)
  1305.         ui->hLineEdit->clear() ;
  1306.     else
  1307.         if (ui->hLineEdit->text().isEmpty())
  1308.         {
  1309.             ui->hLineEdit->setText(QString::fromUtf8("0")) ;
  1310.             newNote.setH(0) ;
  1311.         }
  1312. }
  1313.  
  1314. void AddNote::on_trayCheckBox_stateChanged()
  1315. {
  1316.     if (ui->trayCheckBox->isChecked()) newNote.setTrayDisplay(true) ;
  1317.     else newNote.setTrayDisplay(false) ;
  1318.  
  1319.     enableOK();
  1320. }
  1321.  
  1322. void AddNote::on_windowCheckBox_stateChanged()
  1323. {
  1324.     if (ui->windowCheckBox->isChecked()) newNote.setWindowDisplay(true) ;
  1325.     else newNote.setWindowDisplay(false) ;
  1326.  
  1327.     enableOK();
  1328. }
  1329.  
  1330. bool AddNote::nameInNotes(QString qstrText)
  1331. {
  1332.     for (std::map<std::string, Note>::iterator iter = vNote->begin(); iter != vNote->end(); iter++)
  1333.     {
  1334.         if (iter->second.getTitle() == qstrText) return true ;
  1335.     }
  1336.  
  1337.     return false ;
  1338. }
  1339.  
  1340.  --addnote.h--
  1341.  
  1342. #ifndef ADDNOTE_H
  1343. #define ADDNOTE_H
  1344.  
  1345. #include <QtGui/QDialog>
  1346. #include <QLineEdit>
  1347. #include <QtGui>
  1348.  
  1349. #include <map>
  1350. #include <string>
  1351.  
  1352. #include "note.h"
  1353.  
  1354. class Note ;
  1355.  
  1356. namespace Ui {
  1357. class AddNote;
  1358. }
  1359.  
  1360. class AddNote : public QDialog
  1361. {
  1362.     Q_OBJECT
  1363.    
  1364. public:
  1365.     explicit AddNote(QWidget *parent = 0, std::map<std::string, Note> *nNote = NULL,int sdState = 0) ;
  1366.     ~AddNote();
  1367.    
  1368. private:
  1369.     Ui::AddNote *ui;
  1370.     Note newNote ;
  1371.  
  1372.     std::map<std::string, Note> *vNote;
  1373.  
  1374.     void enableOK () ;
  1375.     void repopCombo () ;
  1376.     bool nameInNotes (QString qstrText) ;
  1377.  
  1378.     std::string IntToString (int IntValue) ;
  1379.     int GetIntVal (std::string StrConvert) ;
  1380.  
  1381. private slots:
  1382.     void on_trayCheckBox_stateChanged () ;
  1383.     void on_windowCheckBox_stateChanged () ;
  1384.  
  1385.     void on_titleLineEdit_editingFinished () ;
  1386.     void on_detailsTextEdit_textChanged () ;
  1387.  
  1388.     void on_hLineEdit_editingFinished () ;
  1389.     void on_mLineEdit_editingFinished () ;
  1390.     void on_sLineEdit_editingFinished () ;
  1391.  
  1392.     void on_sLineEdit_focussed(bool focus) ;
  1393.     void on_mLineEdit_focussed(bool focus) ;
  1394.     void on_hLineEdit_focussed(bool focus) ;
  1395.  
  1396.     void on_okButton_clicked () ;
  1397.     void on_closeButton_clicked() ;
  1398.  
  1399.     void on_fromSetRadio_clicked() ;
  1400.     void on_beforeSdRadio_clicked() ;
  1401.  
  1402.     void on_comboBox_currentIndexChanged (const QString &textBox) ;
  1403.  
  1404.     void on_deleteButton_clicked() ;
  1405.  
  1406. };
  1407.  
  1408. #endif // ADDNOTE_H
  1409.  
  1410.  --focuslineedit.cpp--
  1411.  
  1412. #include "focuslineedit.h"
  1413.  
  1414. FocusLineEdit::FocusLineEdit(QWidget *parent) :
  1415.     QLineEdit(parent)
  1416. {
  1417. }
  1418.  
  1419. FocusLineEdit::~FocusLineEdit()
  1420. {
  1421. }
  1422.  
  1423. void FocusLineEdit::focusInEvent(QFocusEvent *e)
  1424. {
  1425.     QLineEdit::focusInEvent(e) ;
  1426.     emit(focussed(true)) ;
  1427. }
  1428.  
  1429. void FocusLineEdit::focusOutEvent(QFocusEvent *e)
  1430. {
  1431.     QLineEdit::focusOutEvent(e) ;
  1432.     emit(focussed(false)) ;
  1433. }
  1434.  
  1435.  --focuslineedit.h--
  1436.  
  1437. #ifndef FOCUSLINEEDIT_H
  1438. #define FOCUSLINEEDIT_H
  1439.  
  1440. #include <QObject>
  1441. #include <QLineEdit>
  1442.  
  1443. class FocusLineEdit : public QLineEdit
  1444. {
  1445.     Q_OBJECT
  1446.  
  1447. public:
  1448.     explicit FocusLineEdit(QWidget *parent = 0);
  1449.     ~FocusLineEdit() ;
  1450.    
  1451. signals:
  1452.     void focussed (bool hasFocus) ;
  1453.  
  1454. public slots:
  1455.  
  1456. protected:
  1457.     virtual void focusInEvent(QFocusEvent *e) ;
  1458.     virtual void focusOutEvent(QFocusEvent *e) ;
  1459.  
  1460. };
  1461.  
  1462. #endif // FOCUSLINEEDIT_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement