[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
src/klftools/klfpathchooser.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfpathchooser.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2011 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id$ */
00023 
00024 #include <QLineEdit>
00025 #include <QHBoxLayout>
00026 #include <QPushButton>
00027 #include <QFrame>
00028 #include <QFileDialog>
00029 #include <QStandardPaths>
00030 #include <QDirModel>
00031 #include <QCompleter>
00032 
00033 #include "klfpathchooser.h"
00034 
00035 
00036 KLFPathChooser::KLFPathChooser(QWidget *parent)
00037   : QFrame(parent), _mode(0), _caption(), _filter(), _dlgconfirmoverwrite(true),
00038     _pathFromDialog(false)
00039 {
00040   //  setFrameShape(QFrame::Box);
00041   //  setFrameShadow(QFrame::Raised);
00042   setFrameStyle(QFrame::NoFrame|QFrame::Plain);
00043 
00044   QHBoxLayout *lyt = new QHBoxLayout(this);
00045   //  lyt->setContentsMargins(2,2,2,2);
00046   lyt->setContentsMargins(0,0,0,0);
00047   lyt->setSpacing(2);
00048   txtPath = new QLineEdit(this);
00049   lyt->addWidget(txtPath);
00050   btnBrowse = new QPushButton(tr("Browse"), this);
00051   btnBrowse->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
00052   lyt->addWidget(btnBrowse);
00053 
00054   // set a filename completer for txtPath
00055   QDirModel *dirModel = new QDirModel(QStringList(),
00056                                       QDir::AllEntries|QDir::AllDirs|QDir::NoDotAndDotDot,
00057                                       QDir::DirsFirst|QDir::IgnoreCase, this);
00058   QCompleter *fileNameCompleter = new QCompleter(this);
00059   fileNameCompleter->setModel(dirModel);
00060   txtPath->setCompleter(fileNameCompleter);
00061 
00062   // connect signals
00063   connect(txtPath, SIGNAL(textChanged(const QString&)), this, SLOT(slotTextChanged()));
00064   connect(btnBrowse, SIGNAL(clicked()), this, SLOT(requestBrowse()));
00065 }
00066 
00067 KLFPathChooser::~KLFPathChooser()
00068 {
00069 }
00070 
00071 
00072 QString KLFPathChooser::path() const
00073 {
00074   return txtPath->text();
00075 }
00076 
00077 void KLFPathChooser::setPath(const QString& path)
00078 {
00079   txtPath->setText(path);
00080   _pathFromDialog = false;
00081 }
00082 
00083 void KLFPathChooser::requestBrowse()
00084 {
00085   QFileDialog::Options options = 0;
00086   if (_mode == 1 && !_dlgconfirmoverwrite)
00087     options |= QFileDialog::DontConfirmOverwrite;
00088 
00089   QString path;
00090   if (!txtPath->text().isEmpty())
00091     path = txtPath->text();
00092   else {
00093     QStringList docpaths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
00094     if (docpaths.isEmpty()) {
00095       path = "";
00096     } else {
00097       path = docpaths[0];
00098     }
00099   }
00100 
00101   QString s;
00102   if (_mode == 1) {
00103     // save
00104     s = QFileDialog::getSaveFileName(this, _caption, path, _filter, &_selectedfilter, options);
00105   } else if (_mode == 2) {
00106     s = QFileDialog::getExistingDirectory(this, _caption, path, 0/*options*/);
00107   } else {
00108     // open
00109     s = QFileDialog::getOpenFileName(this, _caption, path, _filter, &_selectedfilter);
00110   }
00111   if ( ! s.isEmpty() ) {
00112     setPath(s);
00113     if (_mode == 1 && _dlgconfirmoverwrite)
00114       _pathFromDialog = true;
00115     emit fileDialogPathChosen(s);
00116   }
00117 }
00118 
00119 
00120 void KLFPathChooser::setCaption(const QString& caption)
00121 {
00122   _caption = caption;
00123 }
00124 
00125 void KLFPathChooser::setMode(int mode)
00126 {
00127   _mode = mode;
00128   _pathFromDialog = false; // no overwrite confirmed yet
00129 }
00130 
00131 void KLFPathChooser::setFilter(const QString& filter)
00132 {
00133   _filter = filter;
00134 }
00135 
00136 void KLFPathChooser::slotTextChanged()
00137 {
00138   _pathFromDialog = false;
00139 }
00140 

Generated by doxygen 1.7.6.1. The KLatexFormula website is hosted on sourceforge.net