Česky
Kamil Dudka

emulT9 (C++, Qt4)

File detail

Name:DownloademulT9.cpp [Download]
Location: emulT9 > src
Size:7.4 KB
Last modification:2007-08-27 01:13

Source code

#include <QApplication>
#include <QSplashScreen>
#include <QCloseEvent>
#include <QMessageBox>
#include <QStatusBar>
#include "emulT9.h"
#include "engine.h"
 
EmulT9::EmulT9(QApplication *app):
  _engine (this),       ///< Engine initialization
  _scPause (QKeySequence("esc"), this),
  _bNowWriting (false),
  _bNeedSave (false)
{
  _design.setupUi (this);             ///< Load application design
  _engine.connectToDesign (_design);  ///< Connect engine to main window UI
 
  /// Set timer base to default value. Use designer to change deafult value.
  _engine.setTimer (_design.sldTimer->sliderPosition());
 
  connect (this,  SIGNAL(start()),                              this, SLOT(updateStartPause()));
  connect (this,  SIGNAL(stop()),                               this, SLOT(updateStartPause()));
  connect (app,   SIGNAL(focusChanged(QWidget*,QWidget*)),      this, SLOT(focusChanged(QWidget*,QWidget*)));
 
  /// connect to main window UI
  connect (_design.cbT9,            SIGNAL(toggled(bool)),      this, SLOT(initTimer(bool)));
  connect (_design.cbTimer,         SIGNAL(toggled(bool)),      this, SLOT(initTimerSld(bool)));
  connect (_design.cbPC,            SIGNAL(toggled(bool)),      this, SLOT(initKeyb(bool)));
  connect (_design.sldTimer,        SIGNAL(valueChanged(int)),  this, SLOT(updateTimerSldVal(int)));
  connect (_design.buttStartPause,  SIGNAL(clicked()),          this, SLOT(toggleStartPause()));
  connect (_design.buttReset,       SIGNAL(clicked()),          this, SLOT(tryReset()));
  connect (&_scPause,               SIGNAL(activated()),        this, SLOT(scPause()));
 
  initTimer (true);                   ///< Expects T9 turned on by default.
  initKeyb (true);                    ///< Expects PC keyboard turned on by default.
 
}
void EmulT9::closeEvent (QCloseEvent *event)
{
  if (!_bNeedSave && !_bNowWriting) {
    ///< Nothing to miss actualy.
    event->accept();
    return;
  }
  /// Stop writing now.
  setNowWriting (false);
 
  /// Confirm Exit
  if (QMessageBox::Yes==QMessageBox::warning(
      this,
      tr("emulT9"),
      tr("All data will be lost!\n\nDo you realy want close the application?"),
      QMessageBox::Yes,
      QMessageBox::No | QMessageBox::Escape | QMessageBox::Default))
    event->accept();
  else
    event->ignore();
}
 
void EmulT9::initKeyb (bool bPC)
{
  /// Stop writing now.
  setNowWriting (false);
 
  static const QString k1=QApplication::translate("MainWindow", "1", 0, QApplication::UnicodeUTF8);
  static const QString k2=QApplication::translate("MainWindow", "2", 0, QApplication::UnicodeUTF8);
  static const QString k3=QApplication::translate("MainWindow", "3", 0, QApplication::UnicodeUTF8);
  static const QString k7=QApplication::translate("MainWindow", "7", 0, QApplication::UnicodeUTF8);
  static const QString k8=QApplication::translate("MainWindow", "8", 0, QApplication::UnicodeUTF8);
  static const QString k9=QApplication::translate("MainWindow", "9", 0, QApplication::UnicodeUTF8);
 
  static const QString k10=QApplication::translate("MainWindow", "*", 0, QApplication::UnicodeUTF8);
  static const QString k10pc=QApplication::translate("MainWindow", "0", 0, QApplication::UnicodeUTF8);
  static const QString k11=QApplication::translate("MainWindow", "0", 0, QApplication::UnicodeUTF8);
  static const QString k11pc=QApplication::translate("MainWindow", ".", 0, QApplication::UnicodeUTF8);
  static const QString k12=QApplication::translate("MainWindow", "#", 0, QApplication::UnicodeUTF8);
  static const QString k12pc=QApplication::translate("MainWindow", "+", 0, QApplication::UnicodeUTF8);
  if (bPC) {
    _design.butt11->setText ("7");
    _design.butt11->setShortcut (k7);
    _design.butt12->setText ("8 abc");
    _design.butt12->setShortcut (k8);
    _design.butt13->setText ("9 def");
    _design.butt13->setShortcut (k9);
    _design.butt31->setText ("1 pqrs");
    _design.butt31->setShortcut (k1);
    _design.butt32->setText ("2 tuv");
    _design.butt32->setShortcut (k2);
    _design.butt33->setText ("3 wxyz");
    _design.butt33->setShortcut (k3);
    _design.butt41->setText ("0");
    _design.butt41->setShortcut (k10pc);
    _design.butt42->setText (".");
    _design.butt42->setShortcut (k11pc);
    _design.butt43->setText ("+");
    _design.butt43->setShortcut (k12pc);
  } else {
    _design.butt11->setText ("1");
    _design.butt11->setShortcut (k1);
    _design.butt12->setText ("2 abc");
    _design.butt12->setShortcut (k2);
    _design.butt13->setText ("3 def");
    _design.butt13->setShortcut (k3);
    _design.butt31->setText ("7 pqrs");
    _design.butt31->setShortcut (k7);
    _design.butt32->setText ("8 tuv");
    _design.butt32->setShortcut (k8);
    _design.butt33->setText ("9 wxyz");
    _design.butt33->setShortcut (k9);
    _design.butt41->setText ("*");
    _design.butt41->setShortcut (k10);
    _design.butt42->setText ("0");
    _design.butt42->setShortcut (k11);
    _design.butt43->setText ("#");
    _design.butt43->setShortcut (k12);
  }
}
 
void EmulT9::initTimerSld (bool bShow)
{
  /// Stop writing now.
  setNowWriting (false);
  if (bShow) {
    _engine.setTimer (_design.sldTimer->sliderPosition());
    _design.sldTimer-> show();
    _design.sldTimerVal-> show();
  } else {
    _engine.setTimer (-1);
    _design.sldTimer-> hide();
    _design.sldTimerVal-> hide();
  }
}
 
void EmulT9::toggleStartPause()
{
  setNowWriting (!_bNowWriting);
}
 
void EmulT9::updateStartPause()
{
  /// Update start/pause button text
  QString str= _bNowWriting ? "Pause" : "Start";
  _design.buttStartPause-> setText (str);
  /// Update status bar
  if (_bNowWriting)
    statusBar()->showMessage (tr("Writing..."));
  else
    statusBar()->showMessage (tr("Paused"));
}
 
void EmulT9::setNowWriting(bool bNowWriting)
{
  bool bSave= _bNowWriting;
  /// Set hovered state.
  _bNowWriting= bNowWriting;
  /// Check if state was changed.
  if (_bNowWriting && !bSave)
    emit start();
  else if (!_bNowWriting && bSave)
    emit stop();
}
 
void EmulT9::initTimer (bool bT9)
{
  /// Stop writing now.
  setNowWriting (false);
  _design.cbTimer-> setEnabled (!bT9);
  initTimerSld (!bT9 && _design.cbTimer->isChecked());
}
 
void EmulT9::updateTimerSldVal (int val)
{
  /// Stop writing now.
  setNowWriting (false);
  _engine.setTimer (val);
  _design.sldTimerVal-> setText (QString("%1 ms").arg(val));
}
 
void EmulT9::tryReset()
{
  /// Stop writing now.
  setNowWriting (false);
  if (!_bNeedSave) {
    ///< Nothing to miss at this time.
    emit reset();
    return;
  }
 
  /// Delete all data confirmation.
  if (QMessageBox::Yes!=QMessageBox::warning(
      this,
      tr("emulT9"),
      tr("Clear all text?"),
      QMessageBox::Yes,
      QMessageBox::No | QMessageBox::Escape | QMessageBox::Default))
    return;
 
  _bNeedSave= false;
  emit reset();
}
 
void EmulT9::keyPressed()
{
  _bNeedSave= true;
  setNowWriting (true);
}
 
void EmulT9::focusChanged(QWidget *, QWidget *w)
{
  if (!w || w== _design.textEdit)
    ///< Ooooops, focus lost..
    setNowWriting (false);
}
 
void EmulT9::scPause()
{
  setNowWriting (false);
}
 
int main(int argc, char *argv[])
{
  QApplication app(argc, argv);
  QSplashScreen splash;
  QPixmap pixmap (":images/splash.jpg");
  bool bSplash= !pixmap.isNull();
  if (bSplash) {
    splash.setPixmap (pixmap);
    splash.show();
    splash.showMessage ("Building dictionary...", Qt::AlignLeft, Qt::green);
    app.processEvents();
  }
 
  EmulT9 main (&app); // TODO: Should allocate dynamically?
  main.show();
  if (bSplash)
    splash.finish (&main);
 
  return app.exec();
}