lib/Navigator.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 "Navigator.h"
00023 #include "../pics/dirclosed.xpm"
00024 #include <qpixmap.h>
00025 
00026 Navigator::Navigator (QWidget *w, QString &bp) : QListBox(w)
00027 {
00028   basePath = bp;
00029   id = 0;
00030   keyFlag = FALSE;
00031   selectedFlag = FALSE;
00032 
00033   currentDir.setPath(bp);
00034   currentDir.setMatchAllDirs(TRUE);
00035 
00036   setSelectionMode(QListBox::Single);
00037   /*
00038   connect(this, SIGNAL(currentChanged(QListBoxItem *)), this, SLOT(fileSelection(QListBoxItem *)));
00039   connect(this, SIGNAL(selected(QListBoxItem *)), this, SLOT(checkDirectory(QListBoxItem *)));
00040   */
00041   connect(this, SIGNAL(currentChanged(QListBoxItem *)), this, SLOT(fileSelection(QListBoxItem *)));
00042   
00043   connect(this, SIGNAL(returnPressed (QListBoxItem *)), this, SLOT(checkDirectory(QListBoxItem *)));
00044   
00045   connect(this, SIGNAL(mouseButtonClicked(int, QListBoxItem *, const QPoint &)), this, SLOT(slotMouseClicked(int, QListBoxItem *, const QPoint &)));
00046 
00047   //connect(this, SIGNAL(highlighted(QListBoxItem *)), this, SLOT(highL(QListBoxItem *)));
00048 }
00049 
00050 Navigator::~Navigator ()
00051 {
00052 }
00053 
00054 void Navigator::updateList ()
00055 {
00056   int ci = currentItem();
00057 
00058   clear();
00059 
00060   currentDir.setPath(currentDir.absPath());
00061 
00062   int loop;
00063   if (! basePath.compare(currentDir.absPath()))
00064   {
00065     loop = 2;
00066     currentDir.setFilter(QDir::Dirs);
00067   }
00068   else
00069   {
00070     loop = 1;
00071     currentDir.setFilter(QDir::All);
00072   }
00073 
00074   for (; loop < (int) currentDir.count(); loop++)
00075   {
00076     QString s = currentDir.absPath();
00077     s.append("/");
00078     s.append(currentDir[loop]);
00079     QFileInfo info(s);
00080     
00081     if (info.isDir())
00082     {
00083       if (currentDir[loop].compare("."))
00084         insertItem(QPixmap(dirclosed), currentDir[loop], -1);
00085     }
00086     else
00087     {
00088       // check if this is a broken group link
00089       if (! info.exists())
00090       {
00091         QDir dir;
00092         dir.remove(s);
00093         continue;
00094       }
00095     
00096       insertItem(currentDir[loop], -1);
00097     }
00098   }
00099 
00100   clearSelection();
00101 
00102   if (ci != -1)
00103     setCurrentItem(ci);
00104 }
00105 
00106 void Navigator::setNavItem (QString s)
00107 {
00108   int loop;
00109   for (loop = 0; loop < (int) currentDir.count(); loop++)
00110   {
00111     if ( !s.compare(currentDir[loop]) )
00112     {
00113       setCurrentItem(loop-1); // allow for . and .. dirs
00114       return;
00115     }
00116   }
00117 }
00118 
00119 void Navigator::upDirectory ()
00120 {
00121   QString s = currentDir.dirName();
00122   currentDir.cdUp();
00123   updateList();
00124   setCurrentItem(findItem(s, Qt::ExactMatch));
00125   ensureCurrentVisible();
00126   emit noSelection();
00127   selectedFlag = FALSE;
00128 }
00129 
00130 void Navigator::fileSelection (QListBoxItem *item)
00131 {
00132   //qDebug("fileSelection(currentChanged)");
00133   if (! item)
00134   {
00135     emit noSelection();
00136     selectedFlag = FALSE;
00137     return;
00138   }
00139 
00140   if (item->pixmap())
00141   {
00142     emit noSelection();
00143     selectedFlag = FALSE;
00144     return;
00145   }
00146 
00147   QString s;
00148   getFileSelection(s);
00149   emit fileSelected(s);
00150   selectedFlag = TRUE;
00151 }
00152 
00153 void Navigator::getFileSelection (QString &s)
00154 {
00155   s = currentDir.absPath();
00156   s.append("/");
00157   s.append(currentText());
00158 }
00159 
00160 void Navigator::setDirectory (QString &d)
00161 {
00162   if (d.length())
00163   {
00164   //  QFileInfo fi(d);
00165   //  currentDir.setPath(fi.dirPath(TRUE));
00166     currentDir.setPath(d);
00167     updateList();
00168     emit noSelection();
00169     selectedFlag = FALSE;
00170   }
00171 }
00172 
00173 void Navigator::getCurrentPath (QString &d)
00174 {
00175   d = currentDir.absPath();
00176 }
00177 
00178 void Navigator::checkDirectory (QListBoxItem *item)
00179 {
00180   //qDebug("checkDirectory(selected)");
00181   if (! item)
00182   {
00183     emit noSelection();
00184     selectedFlag = FALSE;
00185     return;
00186   }
00187 
00188   if (! item->text().compare(".."))
00189   {
00190     upDirectory();
00191     return;
00192   }
00193 
00194   QString s;
00195   if (item->pixmap())
00196   { 
00197     // step directory tree down
00198     s = currentDir.absPath() + "/" + item->text();
00199     setDirectory(s);
00200     return;
00201   }
00202   
00203   getFileSelection(s);
00204   emit fileOpened(s);
00205 }
00206 
00207 void Navigator::setFilter (QString &d)
00208 {
00209   currentDir.setNameFilter(d);
00210   updateList();
00211   // If there is only one item, then select it automatically.
00212   if (currentDir.count() == 3)
00213   {
00214     setCurrentItem(1);
00215     QString s1;
00216     getFileSelection(s1);
00217     QFileInfo info(s1);
00218     if (info.isFile())
00219     {
00220       emit fileOpened(s1);
00221       selectedFlag = TRUE;
00222     }
00223   }
00224   else
00225   {
00226     emit noSelection();
00227     selectedFlag = FALSE;
00228   }
00229 }
00230 
00231 void Navigator::setId (int d)
00232 {
00233   id = d;
00234 }
00235 
00236 void Navigator::setKeyFlag (bool d)
00237 {
00238   keyFlag = d;
00239 }
00240 
00241 void Navigator::setHome ()
00242 {
00243   /*QString s = basePath;
00244   currentDir.setPath(s);
00245   updateList();
00246   emit noSelection();
00247   selectedFlag = FALSE;
00248   */
00249   setDirectory(basePath);
00250 }
00251 
00252 bool Navigator::isSelected ()
00253 {
00254   return selectedFlag;
00255 }
00256 
00257 void Navigator::keyPressEvent (QKeyEvent *key)
00258 {
00259   if (keyFlag)
00260     emit signalKeyPressed (id, key->state(), key->key(), key->ascii(), key->text());
00261     
00262   doKeyPress(key);  
00263 }
00264 
00265 void Navigator::doKeyPress (QKeyEvent *key)
00266 {
00267   switch (key->key())
00268   {
00269     case Qt::Key_Delete:
00270       key->accept();
00271       emit keyPress(key->state(), key->key());
00272       break;
00273     case Qt::Key_Left: // segfaults if we dont trap this
00274     case Qt::Key_Right: // segfaults if we dont trap this
00275       key->accept();
00276       break;      
00277     case Qt::Key_Enter:
00278     case Qt::Key_Return:
00279       key->accept();
00280       checkDirectory(item(currentItem()));
00281       break;
00282     case Qt::Key_Home:
00283       key->accept();
00284       setHome();
00285       QListBox::keyPressEvent(key);
00286       break;
00287     default:
00288       key->accept();
00289       QListBox::keyPressEvent(key);
00290       break;
00291   }
00292 }
00293 
00294 /*
00295 void Navigator::highL (QListBoxItem *item)
00296 {
00297 qDebug("highlighted");
00298 }
00299 */
00300 
00301 void Navigator::slotMouseClicked(int btn, QListBoxItem *item, const QPoint &)
00302 {
00303 //qDebug("mouse btn %d clicked", btn);
00304   if(btn == 1) // left button
00305   {
00306     checkDirectory(item);
00307   }
00308 }