Back to index

scribus-ng  1.3.4.dfsg+svn20071115
picsearchoptions.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008                           picsearchoptions.cpp  -  description
00009                              -------------------
00010     begin                : Mon Feb 26 2007
00011     copyright            : (C) 2007 by Franz Schmid
00012     email                : Franz.Schmid@altmuehlnet.de
00013  ***************************************************************************/
00014 
00015 /*
00016 For general Scribus (>=1.3.2) copyright and licensing information please refer
00017 to the COPYING file provided with the program. Following this notice may exist
00018 a copyright and/or license notice that predates the release of Scribus 1.3.2
00019 for which a new license (GPL+exception) is in place.
00020 */
00021 
00022 #include <qmessagebox.h>
00023 #include <qstringlist.h>
00024 #include <qdir.h>
00025 #include <qlineedit.h>
00026 #include <qtooltip.h>
00027 #include <qpushbutton.h>
00028 #include <qtooltip.h>
00029 #include <qcheckbox.h>
00030 #include <qprogressbar.h>
00031 #include <qtimer.h>
00032 #include <qfiledialog.h>
00033 #include <qlabel.h>
00034 #include "picsearchoptions.h"
00035 #include "picsearchoptions.moc"
00036 #include "filesearch.h"
00037 
00038 PicSearchOptions::PicSearchOptions(QWidget* parent, const QString & fileName, const QString & searchBase) : PicSearchOptionsBase( parent, "PicSearchOptions", true, 0 )
00039 {
00040        m_fileName = fileName;
00041        fileEdit->setText(fileName);
00042        directoryEdit->setText(searchBase);
00043        progressBar1->hide();
00044        searchLabel->hide();
00045        connect(startButton, SIGNAL(clicked()), this, SLOT(SearchPic()));
00046        connect(changeDirButton, SIGNAL(clicked()), this, SLOT(changeSearchDir()));
00047        QToolTip::add(caseInsensitiveCheck, "<qt>" + tr("The filesystem will be searched for case insensitive file names when you check this on. Remember it is not default on most operating systems except MS Windows") + "</qt>");
00048 }
00049 
00050 void PicSearchOptions::setSearchButton(bool toCancel, const FileSearch* searcher)
00051 {
00052        if (toCancel)
00053        {
00054               startButton->setText( tr("Cancel Search") );
00055               progressBar1->reset();
00056               progressBar1->show();
00057               progressBar1->setTotalSteps(20);
00058               progressBar1->setProgress(0);
00059               searchLabel->show();
00060               disconnect(startButton, SIGNAL(clicked()), this, SLOT(SearchPic()));
00061               connect(startButton, SIGNAL(clicked()), searcher, SLOT(cancel()));
00062        }
00063        else
00064        {
00065               startButton->setText( tr("Start Search"));
00066               progressBar1->reset();
00067               progressBar1->hide();
00068               searchLabel->hide();
00069               disconnect(startButton, SIGNAL(clicked()), searcher, SLOT(cancel()));
00070               connect(startButton, SIGNAL(clicked()), this, SLOT(SearchPic()));
00071        }
00072 }
00073 
00074 void PicSearchOptions::changeSearchDir()
00075 {
00076        QString workDir;
00077 #ifndef _WIN32
00078        workDir = QDir::homeDirPath();
00079 #endif
00080        QString searchBase;
00081        searchBase = QFileDialog::getExistingDirectory( workDir, NULL, NULL, tr("Select a base directory for search"));
00082        if( searchBase.isEmpty() || !QDir().exists(searchBase) )
00083               return;
00084        directoryEdit->setText(searchBase);
00085 }
00086 
00087 void PicSearchOptions::SearchPic()
00088 {
00089        QString searchBase = directoryEdit->text();
00090        if( searchBase.isEmpty() || !QDir().exists(searchBase) )
00091               return;
00092        // Set up the search, then return to the event loop until it notifies us
00093        // that it's done.
00094        // Note: search will be deleted when this PicStatus is, so there's no
00095        // need to worry about cleanup.
00096        // case sensitive note: it has no meaning on windows
00097        int recurse = -1;
00098        if (recursiveSearch->isChecked())
00099               recurse = -1;
00100        else
00101               recurse = 0;
00102        FileSearch* search = new FileSearch(this, fileEdit->text(), searchBase, recurse, caseInsensitiveCheck->isChecked());
00103        Q_CHECK_PTR(search);
00104        connect(search, SIGNAL(searchComplete(const QStringList&, const QString&)), SLOT(SearchPicFinished(const QStringList&, const QString&)));
00105        connect(search, SIGNAL(aborted(bool)), SLOT(SearchPicAborted(bool)));
00106        // Set up the UI to let the user cancel the search, then start it
00107        setSearchButton(true, search);
00108        QTimer *timer = new QTimer( this );
00109        connect( timer, SIGNAL(timeout()), this, SLOT(timerDone()) );
00110        timer->start(150);
00111        search->start();
00112 }
00113 
00114 void PicSearchOptions::timerDone()
00115 {
00116        int pg = progressBar1->progress();
00117        pg++;
00118        if (pg == 21)
00119        {
00120               progressBar1->reset();
00121               progressBar1->setTotalSteps(20);
00122               progressBar1->setProgress(0);
00123        }
00124        else
00125               progressBar1->setProgress(pg);
00126 }
00127 
00128 void PicSearchOptions::SearchPicAborted(bool userCancelled)
00129 {
00130        m_userCancelled = userCancelled;
00131        const FileSearch* search = dynamic_cast<const FileSearch*>(sender());
00132        Q_ASSERT(search);
00133        setSearchButton(false, search);
00134        if (!userCancelled)
00135               // A running search failed
00136               QMessageBox::warning(this, tr("Scribus - Image Search"), tr("The search failed: %1").arg(search->lastError()),
00137                             QMessageBox::Ok|QMessageBox::Default|QMessageBox::Escape,
00138                             QMessageBox::NoButton);
00139        reject();
00140 }
00141 
00142 void PicSearchOptions::SearchPicFinished(const QStringList & matches, const QString & fileName)
00143 {
00144        m_matches = matches;
00145        m_fileName = fileEdit->text();
00146        accept();
00147 }