src/ScannerPage.cpp

Go to the documentation of this file.
00001 /*
00002  *  Qtstalker stock charter
00003  * 
00004  *  Copyright (C) 2001-2007 Stefan S. Stratigakos
00005  * 
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
00019  *  USA.
00020  */
00021 
00022 #include "ScannerPage.h"
00023 #include "Scanner.h"
00024 #include "SymbolDialog.h"
00025 #include "HelpWindow.h"
00026 #include "../pics/help.xpm"
00027 #include "../pics/open.xpm"
00028 #include "../pics/newchart.xpm"
00029 #include "../pics/delete.xpm"
00030 #include "../pics/rename.xpm"
00031 #include "../pics/macro.xpm"
00032 #include <qinputdialog.h>
00033 #include <qmessagebox.h>
00034 #include <qcursor.h>
00035 #include <qaccel.h>
00036 #include <qfileinfo.h>
00037 #include <qlayout.h>
00038 #include <qtooltip.h>
00039 
00040 ScannerPage::ScannerPage (QWidget *w, DBIndex *i) : QWidget (w)
00041 {
00042   chartIndex = i;
00043   idir.setFilter(QDir::Files);
00044 
00045   QVBoxLayout *vbox = new QVBoxLayout(this);
00046   vbox->setMargin(0);
00047   vbox->setSpacing(5);
00048   
00049   search = new QLineEdit(this);
00050   search->setText("*");
00051   connect(search, SIGNAL(textChanged(const QString &)), this, SLOT(searchChanged(const QString &)));
00052   QToolTip::add(search, tr("List Filter, e.g. s* or sb*"));
00053   vbox->addWidget(search);
00054 
00055   list = new QListBox(this);
00056   connect(list, SIGNAL(contextMenuRequested(QListBoxItem *, const QPoint &)), this,
00057           SLOT(rightClick(QListBoxItem *)));
00058   connect(list, SIGNAL(highlighted(const QString &)), this, SLOT(scannerSelected(const QString &)));
00059   connect(list, SIGNAL(doubleClicked(QListBoxItem *)), this, SLOT(doubleClick(QListBoxItem *)));
00060   vbox->addWidget(list);
00061   
00062   menu = new QPopupMenu(this);
00063   menu->insertItem(QPixmap(newchart), tr("&New Scanner          Ctrl+N"), this, SLOT(newScanner()));
00064   menu->insertItem(QPixmap(open), tr("&Open Scanner             Ctrl+O"), this, SLOT(openScanner()));
00065   menu->insertItem(QPixmap(deleteitem), tr("&Delete Scanner     Ctrl+D"), this, SLOT(deleteScanner()));
00066   menu->insertItem(QPixmap(renam), tr("&Rename Scanner          Ctrl+R"), this, SLOT(renameScanner()));
00067   menu->insertItem(QPixmap(macro), tr("R&un Scanner             Ctrl+U"), this, SLOT(runScanner()));
00068   menu->insertSeparator(-1);
00069   menu->insertItem(QPixmap(help), tr("&Help             Ctrl+H"), this, SLOT(slotHelp()));
00070 
00071   QAccel *a = new QAccel(this);
00072   connect(a, SIGNAL(activated(int)), this, SLOT(slotAccel(int)));
00073   a->insertItem(CTRL+Key_N, NewScanner);
00074   a->insertItem(CTRL+Key_O, OpenScanner);
00075   a->insertItem(CTRL+Key_D, DeleteScanner);
00076   a->insertItem(CTRL+Key_R, RenameScanner);
00077   a->insertItem(CTRL+Key_U, RunScanner);
00078   a->insertItem(CTRL+Key_H, Help);
00079   
00080   refreshList();
00081   scannerSelected(QString());
00082 }
00083 
00084 ScannerPage::~ScannerPage ()
00085 {
00086 }
00087 
00088 void ScannerPage::openScanner ()
00089 {
00090   Scanner *dialog = new Scanner(list->currentText(), chartIndex);
00091   connect(dialog, SIGNAL(exitScanner()), this, SLOT(refreshList()));
00092   connect(dialog, SIGNAL(message(QString)), this, SIGNAL(message(QString)));
00093   connect(dialog, SIGNAL(scanComplete()), this, SIGNAL(refreshGroup()));
00094   dialog->show();
00095 }
00096 
00097 void ScannerPage::openScanner (QString d)
00098 {
00099   Scanner *dialog = new Scanner(d, chartIndex);
00100   connect(dialog, SIGNAL(exitScanner()), this, SLOT(refreshList()));
00101   connect(dialog, SIGNAL(message(QString)), this, SIGNAL(message(QString)));
00102   connect(dialog, SIGNAL(scanComplete()), this, SIGNAL(refreshGroup()));
00103   dialog->show();
00104 }
00105 
00106 void ScannerPage::runScanner ()
00107 {
00108   QString s;
00109   config.getData(Config::ScannerPath, s);
00110   QString s2("*");
00111   SymbolDialog *dialog = new SymbolDialog(this, 
00112                                           s,
00113                                           s,
00114                                           s2,
00115                                           QFileDialog::ExistingFiles);
00116   dialog->setCaption(tr("Select scanners to run"));
00117 
00118   int rc = dialog->exec();
00119 
00120   if (rc == QDialog::Accepted)
00121   {
00122     QStringList l = dialog->selectedFiles();
00123     int loop;
00124     QDir dir;
00125     for (loop = 0; loop < (int) l.count(); loop++)
00126     {
00127       QFileInfo fi(l[loop]);
00128       Scanner *sdialog = new Scanner(fi.fileName(), chartIndex);
00129       connect(sdialog, SIGNAL(exitScanner()), this, SLOT(refreshList()));
00130       connect(sdialog, SIGNAL(message(QString)), this, SIGNAL(message(QString)));
00131       sdialog->show();
00132       sdialog->scan();
00133       delete sdialog;
00134     }
00135   }
00136 
00137   delete dialog;
00138 }
00139 
00140 void ScannerPage::newScanner()
00141 {
00142   bool ok;
00143   QString s = QInputDialog::getText(tr("New Scanner"),
00144                                     tr("Enter new scanner name."),
00145                                     QLineEdit::Normal,
00146                                     tr("NewScanner"),
00147                                     &ok,
00148                                     this);
00149   if ((ok) && (! s.isNull()))
00150   {
00151     int loop;
00152     QString selection;
00153     for (loop = 0; loop < (int) s.length(); loop++)
00154     {
00155       QChar c = s.at(loop);
00156       if (c.isLetterOrNumber())
00157         selection.append(c);
00158     }
00159   
00160     config.getData(Config::ScannerPath, s);
00161     s.append("/" + selection);
00162     QDir dir(s);
00163     if (dir.exists(s, TRUE))
00164     {
00165       QMessageBox::information(this, tr("Qtstalker: Error"), tr("This scanner already exists."));
00166       return;
00167     }
00168 
00169     openScanner(selection);
00170   }
00171 }
00172 
00173 void ScannerPage::deleteScanner()
00174 {
00175   QString s;
00176   config.getData(Config::ScannerPath, s);
00177   QString s2("*");
00178   SymbolDialog *dialog = new SymbolDialog(this,
00179                                           s,
00180                                           s,
00181                                           s2,
00182                                           QFileDialog::ExistingFiles);
00183   dialog->setCaption(tr("Select Scanners To Delete"));
00184 
00185   int rc = dialog->exec();
00186 
00187   if (rc == QDialog::Accepted)
00188   {
00189     rc = QMessageBox::warning(this,
00190                               tr("Qtstalker: Warning"),
00191                               tr("Are you sure you want to delete this scanner?"),
00192                               QMessageBox::Yes,
00193                               QMessageBox::No,
00194                               QMessageBox::NoButton);
00195 
00196     if (rc == QMessageBox::No)
00197     {
00198       delete dialog;
00199       return;
00200     }
00201 
00202     QStringList l = dialog->selectedFiles();
00203     int loop;
00204     QDir dir;
00205     for (loop = 0; loop < (int) l.count(); loop++)
00206       dir.remove(l[loop], TRUE);
00207 
00208     refreshList();
00209     scannerSelected(QString());
00210   }
00211 
00212   delete dialog;
00213 }
00214 
00215 void ScannerPage::renameScanner ()
00216 {
00217   bool ok;
00218   QString s = QInputDialog::getText(tr("Rename Scanner"),
00219                                     tr("Enter new scanner name."),
00220                                     QLineEdit::Normal,
00221                                     list->currentText(),
00222                                     &ok,
00223                                     this);
00224   if ((ok) && (! s.isNull()))
00225   {
00226     int loop;
00227     QString selection;
00228     for (loop = 0; loop < (int) s.length(); loop++)
00229     {
00230       QChar c = s.at(loop);
00231       if (c.isLetterOrNumber())
00232         selection.append(c);
00233     }
00234   
00235     config.getData(Config::ScannerPath, s);
00236     s.append("/" + selection);
00237     QDir dir(s);
00238     if (dir.exists(s, TRUE))
00239     {
00240       QMessageBox::information(this, tr("Qtstalker: Error"), tr("This scanner already exists."));
00241       return;
00242     }
00243 
00244     QString s2;
00245     config.getData(Config::ScannerPath, s2);
00246     s2.append("/" + list->currentText());
00247 
00248     dir.rename(s2, s, TRUE);
00249 
00250     refreshList();
00251     scannerSelected(QString());
00252   }
00253 }
00254 
00255 void ScannerPage::scannerSelected (const QString &d)
00256 {
00257   if (d.length())
00258   {
00259     menu->setItemEnabled(menu->idAt(1), TRUE);
00260     menu->setItemEnabled(menu->idAt(2), TRUE);
00261     menu->setItemEnabled(menu->idAt(3), TRUE);
00262   }
00263   else
00264   {
00265     menu->setItemEnabled(menu->idAt(1), FALSE);
00266     menu->setItemEnabled(menu->idAt(2), FALSE);
00267     menu->setItemEnabled(menu->idAt(3), FALSE);
00268   }
00269 }
00270 
00271 void ScannerPage::rightClick (QListBoxItem *)
00272 {
00273   menu->exec(QCursor::pos());
00274 }
00275 
00276 void ScannerPage::refreshList ()
00277 {
00278   list->clear();
00279 
00280   QString s;
00281   config.getData(Config::ScannerPath, s);
00282   idir.setPath(s);
00283   int loop;
00284   for (loop = 0; loop < (int) idir.count(); loop++)
00285     list->insertItem(idir[loop], -1);
00286 }
00287 
00288 void ScannerPage::doubleClick (QListBoxItem *item)
00289 {
00290   if (! item)
00291     return;
00292     
00293   openScanner(item->text());
00294 }
00295 
00296 void ScannerPage::slotHelp ()
00297 {
00298   QString s = "workwithscanner.html";
00299   HelpWindow *hw = new HelpWindow(this, s);
00300   hw->show();
00301 }
00302 
00303 void ScannerPage::keyPressEvent (QKeyEvent *key)
00304 {
00305   doKeyPress(key);  
00306 }
00307 
00308 void ScannerPage::doKeyPress (QKeyEvent *key)
00309 {
00310   key->accept();
00311   
00312   if (key->state() == Qt::ControlButton)
00313   {
00314     switch(key->key())
00315     {
00316       case Qt::Key_N:
00317         slotAccel(NewScanner);
00318         break;
00319       case Qt::Key_D:
00320         slotAccel(DeleteScanner);
00321         break;
00322       case Qt::Key_O:
00323         slotAccel(OpenScanner);
00324         break;
00325       case Qt::Key_R:
00326         slotAccel(RenameScanner);
00327         break;
00328       default:
00329         break;
00330     }
00331   }
00332   else
00333   {
00334     switch (key->key())
00335     {
00336       case Qt::Key_Delete:
00337         deleteScanner();
00338         break;
00339       case Qt::Key_Left: // segfaults if we dont trap this
00340       case Qt::Key_Right: // segfaults if we dont trap this
00341         break;      
00342       case Qt::Key_Enter:
00343       case Qt::Key_Return:
00344         openScanner();
00345         break;
00346       default:
00347 //        list->keyPressEvent(key);
00348         break;
00349     }
00350   }
00351 }
00352 
00353 void ScannerPage::slotAccel (int id)
00354 {
00355   switch (id)
00356   {
00357     case NewScanner:
00358       newScanner();
00359       break;  
00360     case DeleteScanner:
00361       deleteScanner();
00362       break;  
00363     case RenameScanner:
00364       renameScanner();
00365       break;  
00366     case OpenScanner:
00367       openScanner();
00368       break;  
00369     case RunScanner:
00370       runScanner();
00371       break;  
00372     case Help:
00373       slotHelp();
00374       break;  
00375     default:
00376       break;
00377   }
00378 }
00379 
00380 void ScannerPage::searchChanged (const QString &d)
00381 {
00382   idir.setNameFilter(d);
00383   refreshList();
00384 }
00385