src/GroupPage.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 "GroupPage.h"
00023 #include "SymbolDialog.h"
00024 #include "HelpWindow.h"
00025 #include "Traverse.h"
00026 #include "../pics/help.xpm"
00027 #include "../pics/delete.xpm"
00028 #include "../pics/delgroup.xpm"
00029 #include "../pics/newchart.xpm"
00030 #include "../pics/insert.xpm"
00031 #include "../pics/rename.xpm"
00032 #include <qmessagebox.h>
00033 #include <qlineedit.h>
00034 #include <qinputdialog.h>
00035 #include <qstringlist.h>
00036 #include <qcursor.h>
00037 #include <stdlib.h>
00038 #include <qtooltip.h>
00039 #include <qlayout.h>
00040 #include <qfileinfo.h>
00041 #include <qaccel.h>
00042 
00043 GroupPage::GroupPage (QWidget *w) : QWidget (w)
00044 {
00045   QVBoxLayout *vbox = new QVBoxLayout(this);
00046   vbox->setMargin(0);
00047   vbox->setSpacing(5);
00048 
00049   group = new QLineEdit(this);
00050   group->setReadOnly(TRUE);
00051   QToolTip::add(group, tr("Current Group"));
00052   group->setFocusPolicy(QWidget::NoFocus);
00053   vbox->addWidget(group);
00054 
00055   QString s;
00056   rcfile.loadData(RcFile::GroupPath, s);
00057   nav = new Navigator(this, s);
00058   connect(nav, SIGNAL(fileSelected(QString)), this, SLOT(groupSelected(QString)));
00059   connect(nav, SIGNAL(fileOpened(QString)), this, SLOT(chartOpened(QString)));
00060   connect(nav, SIGNAL(noSelection()), this, SLOT(groupNoSelection()));
00061   connect(nav, SIGNAL(contextMenuRequested(QListBoxItem *, const QPoint &)), this, SLOT(rightClick(QListBoxItem *)));
00062     
00063   vbox->addWidget(nav);
00064 
00065   menu = new QPopupMenu(this);
00066   menu->insertItem(QPixmap(newchart), tr("&New Group            Ctrl+N"), this, SLOT(newGroup()));
00067   menu->insertItem(QPixmap(insert), tr("&Add Group Items        Ctrl+A"), this, SLOT(addGroupItem()));
00068   menu->insertItem(QPixmap(deleteitem), tr("&Delete Group Items Ctrl+D"), this, SLOT(deleteGroupItem()));
00069   menu->insertItem(QPixmap(delgroup), tr("De&lete Group Ctrl+L"), this, SLOT(deleteGroup()));
00070   menu->insertItem(QPixmap(renam), tr("&Rename Group    Ctrl+R"), this, SLOT(renameGroup()));
00071   menu->insertSeparator(-1);
00072   menu->insertItem(QPixmap(help), tr("&Help             Ctrl+H"), this, SLOT(slotHelp()));
00073 
00074   QAccel *a = new QAccel(this);
00075   connect(a, SIGNAL(activated(int)), this, SLOT(slotAccel(int)));
00076   a->insertItem(CTRL+Key_N, NewGroup);
00077   a->insertItem(CTRL+Key_A, AddGroupItem);
00078   a->insertItem(CTRL+Key_D, DeleteGroupItem);
00079   a->insertItem(CTRL+Key_L, DeleteGroup);
00080   a->insertItem(CTRL+Key_R, RenameGroup);
00081   a->insertItem(CTRL+Key_H, Help);
00082   a->insertItem(Key_Delete, DeleteChart);
00083   
00084   rcfile.loadData(RcFile::LastGroupUsed, s);
00085   if (!s.isEmpty()) 
00086     nav->setDirectory(s);
00087   else 
00088     nav->updateList();
00089 }
00090 
00091 GroupPage::~GroupPage ()
00092 {
00093   QString s;
00094   nav->getCurrentPath(s);
00095   rcfile.saveData(RcFile::LastGroupUsed,s);
00096 }
00097 
00098 void GroupPage::newGroup()
00099 {
00100   bool ok;
00101   QString s = QInputDialog::getText(tr("New Group"),
00102                                     tr("Enter new group symbol."),
00103                                     QLineEdit::Normal,
00104                                     tr("NewGroup"),
00105                                     &ok,
00106                                     this);
00107   if ((! ok) || (s.isNull()))
00108     return;
00109 
00110   int loop;
00111   QString selection;
00112   for (loop = 0; loop < (int) s.length(); loop++)
00113   {
00114     QChar c = s.at(loop);
00115     if (c.isLetterOrNumber())
00116       selection.append(c);
00117   }
00118   
00119   nav->getCurrentPath(s);
00120   s.append("/" + selection);
00121   QDir dir(s);
00122   if (dir.exists(s, TRUE))
00123   {
00124     QMessageBox::information(this, tr("Qtstalker: Error"), tr("This group already exists."));
00125     return;
00126   }
00127 
00128   dir.mkdir(s, TRUE);
00129   nav->updateList();
00130 }
00131 
00132 void GroupPage::addGroupItem()
00133 {
00134   QString s("*");
00135   QString s2;
00136   rcfile.loadData(RcFile::DataPath, s2);
00137   SymbolDialog *dialog = new SymbolDialog(this,
00138                                           s2,
00139                                           s2,
00140                                           s,
00141                                           QFileDialog::ExistingFiles);
00142 
00143   int rc = dialog->exec();
00144 
00145   if (rc == QDialog::Accepted)
00146   {
00147     int loop;
00148     QStringList l = dialog->selectedFiles();
00149     for (loop = 0; loop < (int) l.count(); loop++)
00150     {
00151       QFileInfo fi(l[loop]);
00152     
00153       s = "ln -s \"" + l[loop] + "\" ";
00154       QString ts;
00155       nav->getCurrentPath(ts);
00156       QString s2 = ts + "/\"" + fi.fileName() + "\"";
00157       QDir dir;
00158       dir.remove(s2, TRUE);
00159       s.append(s2);
00160       system(s);
00161     }
00162 
00163     nav->updateList();
00164   }
00165 
00166   delete dialog;
00167 }
00168 
00169 void GroupPage::deleteGroupItem()
00170 {
00171   QString s("*");
00172   QString s2;
00173   nav->getCurrentPath(s2);
00174   SymbolDialog *dialog = new SymbolDialog(this,
00175                                           s2,
00176                                           s2,
00177                                           s,
00178                                           QFileDialog::ExistingFiles);
00179   dialog->setCaption(tr("Select Group Items To Delete"));
00180 
00181   int rc = dialog->exec();
00182 
00183   if (rc == QDialog::Accepted)
00184   {
00185     rc = QMessageBox::warning(this,
00186                               tr("Qtstalker: Warning"),
00187                               tr("Are you sure you want to delete group items?"),
00188                               QMessageBox::Yes,
00189                               QMessageBox::No,
00190                               QMessageBox::NoButton);
00191 
00192     if (rc == QMessageBox::No)
00193     {
00194       delete dialog;
00195       return;
00196     }
00197 
00198     QStringList l = dialog->selectedFiles();
00199     int loop;
00200     QDir dir;
00201     for (loop = 0; loop < (int) l.count(); loop++)
00202     {
00203       if (! dir.remove(l[loop], TRUE))
00204         qDebug("GroupPage::deleteGroupItem:failed to delete symlink");
00205     }
00206 
00207     nav->updateList();
00208 
00209     groupNoSelection();
00210 
00211     if (l.count())
00212       emit removeRecentCharts(l);
00213   }
00214   
00215   delete dialog;
00216 }
00217 
00218 void GroupPage::deleteChart()
00219 {
00220   if (! nav->isSelected())
00221     return;
00222 
00223   QString s;
00224   nav->getFileSelection(s);
00225   if (! s.length())
00226     return;
00227 
00228   int rc = QMessageBox::warning(this,
00229                             tr("Qtstalker: Warning"),
00230                             tr("Are you sure you want to delete group item?"),
00231                             QMessageBox::Yes,
00232                             QMessageBox::No,
00233                             QMessageBox::NoButton);
00234 
00235   if (rc == QMessageBox::No)
00236     return;
00237 
00238   QDir dir;
00239   dir.remove(s, TRUE);
00240   nav->updateList();
00241   groupNoSelection();
00242 }
00243 
00244 void GroupPage::deleteGroup()
00245 {
00246   QString s;
00247   nav->getCurrentPath(s);
00248   QFileInfo fi(s);
00249   s = tr("Are you sure you want to delete current group ");
00250   s.append(fi.fileName());
00251   int rc = QMessageBox::warning(this,
00252                                 tr("Qtstalker: Warning"),
00253                                 s,
00254                                 QMessageBox::Yes,
00255                                 QMessageBox::No,
00256                                 QMessageBox::NoButton);
00257 
00258   if (rc == QMessageBox::No)
00259     return;
00260 
00261   nav->getCurrentPath(s);
00262   QString ts;
00263   rcfile.loadData(RcFile::GroupPath, ts);
00264   if (! s.compare(ts))
00265     return;
00266   
00267   s =  "rm -r ";
00268   nav->getCurrentPath(ts);
00269   s.append(ts);
00270   system(s);
00271   nav->upDirectory();
00272   nav->updateList();
00273   groupNoSelection();
00274 }
00275 
00276 void GroupPage::renameGroup ()
00277 {
00278   QString s;
00279   nav->getCurrentPath(s);
00280   QFileInfo fi(s);
00281   bool ok;
00282   s = QInputDialog::getText(tr("Rename Group"),
00283                             tr("Enter new group symbol."),
00284                             QLineEdit::Normal,
00285                             fi.fileName(),
00286                             &ok,
00287                             this);
00288   if ((! ok) || (s.isNull()))
00289     return;
00290 
00291   int loop;
00292   QString selection;
00293   for (loop = 0; loop < (int) s.length(); loop++)
00294   {
00295     QChar c = s.at(loop);
00296     if (c.isLetterOrNumber())
00297       selection.append(c);
00298   }
00299         
00300   nav->getCurrentPath(s);
00301     
00302   QStringList l = QStringList::split("/", s, FALSE);
00303   l[l.count() - 1] = selection;
00304   QString s2 = l.join("/");
00305   s2.prepend("/");
00306   
00307   QDir dir(s2);
00308   if (dir.exists(s2, TRUE))
00309   {
00310     QMessageBox::information(this, tr("Qtstalker: Error"), tr("This chart group exists."));
00311     return;
00312   }
00313 
00314   if (! dir.rename(s, s2, TRUE))
00315   {
00316     QMessageBox::information(this, tr("Qtstalker: Error"), tr("Group rename failed."));
00317     return;
00318   }
00319 
00320   nav->setDirectory(s2);
00321   nav->updateList();
00322   
00323   groupNoSelection();
00324 }
00325 
00326 void GroupPage::groupSelected (QString)
00327 {
00328   menu->setItemEnabled(menu->idAt(1), TRUE);
00329   menu->setItemEnabled(menu->idAt(2), TRUE);
00330   menu->setItemEnabled(menu->idAt(3), TRUE);
00331   menu->setItemEnabled(menu->idAt(4), TRUE);
00332 }
00333 
00334 void GroupPage::chartOpened (QString d)
00335 {
00336   QFileInfo fi(d);
00337   emit fileSelected(fi.readLink());
00338   emit addRecentChart(d);
00339 }
00340 
00341 void GroupPage::groupNoSelection ()
00342 {
00343   QString s, s2;
00344   rcfile.loadData(RcFile::GroupPath, s);
00345   nav->getCurrentPath(s2);
00346   if (s.compare(s2))
00347   {
00348     group->setText(s2.right(s2.length() - s.length() - 1));
00349 
00350     menu->setItemEnabled(menu->idAt(1), TRUE);
00351     menu->setItemEnabled(menu->idAt(2), TRUE);
00352     menu->setItemEnabled(menu->idAt(3), TRUE);
00353     menu->setItemEnabled(menu->idAt(4), TRUE);
00354   }
00355   else
00356   {
00357     group->clear();
00358 
00359     menu->setItemEnabled(menu->idAt(1), FALSE);
00360     menu->setItemEnabled(menu->idAt(2), FALSE);
00361     menu->setItemEnabled(menu->idAt(3), FALSE);
00362     menu->setItemEnabled(menu->idAt(4), FALSE);
00363   }
00364 }
00365 
00366 void GroupPage::rightClick (QListBoxItem *)
00367 {
00368   menu->exec(QCursor::pos());
00369 }
00370 
00371 void GroupPage::slotHelp ()
00372 {
00373   QString s = "workwithgroups.html";
00374   HelpWindow *hw = new HelpWindow(this, s);
00375   hw->show();
00376 }
00377 
00378 void GroupPage::setFocus ()
00379 {
00380   nav->setFocus();
00381 }
00382 
00383 void GroupPage::addChartToGroup (QString symbol)
00384 {
00385   QString s;
00386   rcfile.loadData(RcFile::GroupPath, s);
00387   Traverse t(Traverse::Dir);
00388   t.traverse(s);
00389   QStringList l;
00390   t.getList(l);
00391 
00392   bool ok;
00393   s = QInputDialog::getItem(QObject::tr("Add To Group Selection"),
00394                             QObject::tr("Select a group"),
00395                             l,
00396                             0,
00397                             TRUE,
00398                             &ok,
00399                             this);
00400   if (! ok)
00401     return;
00402 
00403   QFileInfo fi(symbol);
00404   QString str = "ln -s \"" + symbol + "\" ";
00405   QString s2 = s + "/\"" + fi.fileName() + "\"";
00406   QDir dir;
00407   dir.remove(s2, TRUE);
00408   str.append(s2);
00409   system(str);
00410 
00411   nav->updateList();
00412 }
00413 
00414 void GroupPage::doKeyPress (QKeyEvent *key)
00415 {
00416   key->accept();
00417   
00418   if (key->state() == Qt::ControlButton)
00419   {
00420     switch(key->key())
00421     {
00422       case Qt::Key_N:
00423         slotAccel(NewGroup);
00424         break;
00425       case Qt::Key_A:
00426         slotAccel(AddGroupItem);
00427         break;
00428       case Qt::Key_D:
00429         slotAccel(DeleteGroupItem);
00430         break;
00431       case Qt::Key_L:
00432         slotAccel(DeleteGroup);
00433         break;
00434       case Qt::Key_R:
00435         slotAccel(RenameGroup);
00436         break;
00437       case Qt::Key_H:
00438         slotAccel(Help);
00439         break;
00440       default:
00441         break;
00442     }
00443   }
00444   else
00445   {
00446     switch(key->key())
00447     {
00448       case Qt::Key_Delete:
00449         slotAccel(DeleteChart);
00450         break;
00451       default:
00452         nav->doKeyPress(key);
00453         break;
00454     }
00455   }
00456 }
00457 
00458 void GroupPage::slotAccel (int id)
00459 {
00460   switch (id)
00461   {
00462     case DeleteChart:
00463       deleteChart();
00464       break;  
00465     case NewGroup:
00466       newGroup();
00467       break;  
00468     case AddGroupItem:
00469       addGroupItem();
00470       break;  
00471     case DeleteGroupItem:
00472       deleteGroupItem();
00473       break;  
00474     case DeleteGroup:
00475       deleteGroup();
00476       break;  
00477     case Help:
00478       slotHelp();
00479       break;  
00480     case RenameGroup:
00481       renameGroup();
00482       break;
00483     default:
00484       break;
00485   }
00486 }
00487 
00488 void GroupPage::setGroupNavItem (QString chartDir, QString chartName)
00489 {
00490   nav->setDirectory(chartDir);
00491   nav->setNavItem(chartName);
00492 }
00493 
00494 void GroupPage::refreshList ()
00495 {
00496   nav->updateList();
00497 }
00498