00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00039
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
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
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);
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
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
00165
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
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
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
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
00244
00245
00246
00247
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:
00274 case Qt::Key_Right:
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
00296
00297
00298
00299
00300
00301 void Navigator::slotMouseClicked(int btn, QListBoxItem *item, const QPoint &)
00302 {
00303
00304 if(btn == 1)
00305 {
00306 checkDirectory(item);
00307 }
00308 }