"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2bFileDialog.cpp" (12 Feb 2021, 7033 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "c2bFileDialog.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0.0_vs_2.0.1.

    1 /***************************************************************************
    2  *   Copyright (C) 2004-2021 by Pere Constans
    3  *   constans@molspaces.com
    4  *   cb2Bib version 2.0.1. Licensed under the GNU GPL version 3.
    5  *   See the LICENSE file that comes with this distribution.
    6  ***************************************************************************/
    7 #include "c2bFileDialog.h"
    8 #include "c2bSettings.h"
    9 
   10 #include <QLineEdit>
   11 #include <QTimer>
   12 
   13 
   14 c2bFileDialog::c2bFileDialog(QWidget* parentw, const QString& caption, const QString& fpath, const QString& ffilter)
   15     : QFileDialog(parentw, caption, fpath, ffilter)
   16 {
   17     setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
   18     setWindowFlags(windowFlags() | Qt::WindowMinMaxButtonsHint);
   19     setOptions(QFileDialog::DontUseNativeDialog);
   20     setModal(true);
   21     settings = c2bSettingsP;
   22 }
   23 
   24 c2bFileDialog::~c2bFileDialog() {}
   25 
   26 
   27 QString c2bFileDialog::getFilename(QWidget* parentw, const QString& caption, const QString& path, const QString& filter)
   28 {
   29     c2bFileDialog* dlg(new c2bFileDialog(parentw, _caption(tr("Select a filename"), caption), _path(path), filter));
   30     dlg->setFileMode(AnyFile);
   31     dlg->setOption(QFileDialog::DontConfirmOverwrite, true);
   32     dlg->setAcceptMode(AcceptSave);
   33     dlg->setLabelText(QFileDialog::Accept, "Select");
   34     const QString fn(dlg->fileName(path));
   35     delete dlg;
   36     return fn;
   37 }
   38 
   39 QString c2bFileDialog::getOpenFilename(QWidget* parentw, const QString& caption, const QString& path,
   40                                        const QString& filter)
   41 {
   42     c2bFileDialog* dlg(new c2bFileDialog(parentw, _caption(tr("Select a filename"), caption), _path(path), filter));
   43     dlg->setFileMode(ExistingFile);
   44     dlg->setLabelText(QFileDialog::Accept, "Select");
   45     const QString fn(dlg->fileName(path));
   46     delete dlg;
   47     return fn;
   48 }
   49 
   50 QStringList c2bFileDialog::getOpenFilenames(QWidget* parentw, const QString& caption, const QString& path,
   51         const QString& filter)
   52 {
   53     c2bFileDialog* dlg(new c2bFileDialog(parentw, _caption(tr("Select filenames"), caption), _path(path), filter));
   54     dlg->setFileMode(ExistingFiles);
   55     dlg->setLabelText(QFileDialog::Accept, "Select");
   56     const QString fn(dlg->fileName(path));
   57     QStringList fns;
   58     if (!fn.isEmpty())
   59         fns = dlg->selectedFiles();
   60     delete dlg;
   61     return fns;
   62 }
   63 
   64 QString c2bFileDialog::getSaveFilename(QWidget* parentw, const QString& caption, const QString& path,
   65                                        const QString& filter)
   66 {
   67     c2bFileDialog* dlg(new c2bFileDialog(parentw, _caption(tr("Save As"), caption), _path(path), filter));
   68     dlg->setFileMode(AnyFile);
   69     dlg->setAcceptMode(AcceptSave);
   70     const QString fn(dlg->fileName(path));
   71     delete dlg;
   72     return fn;
   73 }
   74 
   75 QString c2bFileDialog::getExistingDirectory(QWidget* parentw, const QString& caption, const QString& path)
   76 {
   77     c2bFileDialog* dlg(new c2bFileDialog(parentw, _caption("Select directory", caption), _dirpath(path), QString()));
   78     dlg->setFileMode(Directory);
   79     dlg->setOption(QFileDialog::ShowDirsOnly, true);
   80     dlg->setLabelText(QFileDialog::Accept, "Select");
   81     const QString fn(dlg->fileName(QString()));
   82     delete dlg;
   83     return fn;
   84 }
   85 
   86 QString c2bFileDialog::getSystemFilename(QWidget* parentw, const QString& caption, const QString& path,
   87         const QString& filter)
   88 {
   89     c2bFileDialog* dlg(new c2bFileDialog(parentw, _caption(tr("Select a filename"), caption), _path(path), filter));
   90     dlg->setFileMode(AnyFile);
   91     dlg->setLabelText(QFileDialog::Accept, "Select");
   92     dlg->setFilter(QDir::Drives | QDir::AllDirs | QDir::NoDotAndDotDot | QDir::System);
   93     dlg->setOption(QFileDialog::HideNameFilterDetails, true);
   94     const QString fn(dlg->fileName(path));
   95     delete dlg;
   96     return fn;
   97 }
   98 
   99 QString c2bFileDialog::fileName(const QString& path)
  100 {
  101     readSettings();
  102     _current_file = path;
  103     const QStringList fnf(nameFilters().filter(QFileInfo(path).completeSuffix() + ')', Qt::CaseInsensitive));
  104     if (fnf.count() > 0)
  105         selectNameFilter(fnf.at(0));
  106     QTimer::singleShot(250, this, SLOT(selectCurrentFile()));
  107     QString sfn;
  108     if (QFileDialog::exec() == QDialog::Accepted)
  109     {
  110         const QStringList selected(selectedFiles());
  111         if (selected.count() > 0)
  112             sfn = selected.first();
  113     }
  114     writeSettings();
  115     return QDir::toNativeSeparators(QDir::cleanPath(sfn));
  116 }
  117 
  118 void c2bFileDialog::accept()
  119 {
  120     // Set suffix for getSaveFilename dialog
  121     if (acceptMode() == AcceptSave)
  122     {
  123         QString suffix(selectedNameFilter());
  124         QRegExp sre("\\*\\.([\\w\\.]+)");
  125         if (sre.indexIn(suffix) != -1)
  126         {
  127             suffix = sre.cap(1);
  128             setDefaultSuffix(suffix);
  129         }
  130     }
  131     QFileDialog::accept();
  132 }
  133 
  134 void c2bFileDialog::selectCurrentFile()
  135 {
  136     // Bug fix for Qt 4.4 not giving correct selection
  137     const QString fn(_filename(_current_file));
  138     QLineEdit* le = findChild<QLineEdit*>();
  139     if (le)
  140     {
  141         le->setText(fn);
  142         le->selectAll();
  143     }
  144 }
  145 
  146 void c2bFileDialog::readSettings()
  147 {
  148     // Size
  149     resize(settings->value("c2bFileDialog/size", sizeHint()).toSize());
  150 
  151     // History
  152     QStringList dhistory(settings->value("c2bFileDialog/history").toStringList());
  153     dhistory.removeAll(directory().absolutePath());
  154     std::sort(dhistory.begin(), dhistory.end());
  155     setHistory(dhistory);
  156 }
  157 
  158 void c2bFileDialog::writeSettings()
  159 {
  160     // Size
  161     settings->setValue("c2bFileDialog/size", size());
  162 
  163     // History
  164     if (result() == QDialog::Accepted)
  165     {
  166         // Unique paths, keeping the newest history entries
  167         QStringList dhistory(history());
  168         dhistory.append(directory().absolutePath());
  169         dhistory.removeAll(QDir::homePath());
  170         dhistory.removeAll(QDir::rootPath());
  171         QStringList chronological(settings->value("c2bFileDialog/history").toStringList());
  172         chronological.append(dhistory.last());
  173         // Discard repeated
  174         dhistory.clear();
  175         for (int i = chronological.count() - 1; i > -1; --i)
  176             if (!dhistory.contains(chronological.at(i)))
  177             {
  178                 dhistory.prepend(chronological.at(i));
  179                 if (dhistory.count() > 10)
  180                     break;
  181             }
  182         settings->setValue("c2bFileDialog/history", dhistory);
  183     }
  184 }
  185 
  186 QString c2bFileDialog::_caption(const QString& generic, const QString& caption)
  187 {
  188     if (caption.isEmpty())
  189         return generic + " - cb2Bib";
  190     else
  191         return caption + " - cb2Bib";
  192 }
  193 
  194 QString c2bFileDialog::_filename(const QString& path)
  195 {
  196     if (path.isEmpty())
  197         return path;
  198     else
  199         return QFileInfo(path).fileName();
  200 }
  201 
  202 QString c2bFileDialog::_path(const QString& path)
  203 {
  204     if (path.isEmpty())
  205         return QDir::homePath();
  206     else
  207         return QFileInfo(path).absolutePath();
  208 }
  209 
  210 QString c2bFileDialog::_dirpath(const QString& path)
  211 {
  212     if (path.isEmpty())
  213         return QDir::homePath();
  214     else
  215         return QFileInfo(path + '/').absolutePath();
  216 }