File: c2bFileDialog.cpp

package info (click to toggle)
cb2bib 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,368 kB
  • sloc: cpp: 24,179; sh: 481; makefile: 16
file content (242 lines) | stat: -rw-r--r-- 7,376 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/***************************************************************************
 *   Copyright (C) 2004-2015 by Pere Constans
 *   constans@molspaces.com
 *   cb2Bib version 1.9.2. Licensed under the GNU GPL version 3.
 *   See the LICENSE file that comes with this distribution.
 ***************************************************************************/
#include "c2bFileDialog.h"
#include "c2bSettings.h"

#include <QDialogButtonBox>
#include <QLineEdit>
#include <QPushButton>
#include <QTimer>


c2bFileDialog::c2bFileDialog(QWidget* parentw, const QString& caption, const QString& fpath, const QString& ffilter)
    : QFileDialog(parentw, caption, fpath, ffilter)
{
    setModal(true);
    settings = c2bSettingsP;
#ifdef Q_OS_UNIX
    // Distinguish executable files
    fip = new fileIconProvider();
    setIconProvider(fip);
#endif
}

c2bFileDialog::~c2bFileDialog()
{
#ifdef Q_OS_UNIX
    delete fip;
#endif
}


QString c2bFileDialog::getFilename(QWidget* parentw, const QString& caption, const QString& path, const QString& filter)
{
    c2bFileDialog* dlg = new c2bFileDialog(parentw, _caption(tr("Select a filename"), caption), _path(path), filter);
    dlg->setConfirmOverwrite(false);
    dlg->setFileMode(AnyFile);
    dlg->setAcceptMode(AcceptSave);
    dlg->setLabelText(QFileDialog::Accept, "Select");
    const QString fn(dlg->exec(path));
    delete dlg;
    return fn;
}

QString c2bFileDialog::getOpenFilename(QWidget* parentw, const QString& caption, const QString& path, const QString& filter)
{
    c2bFileDialog* dlg = new c2bFileDialog(parentw, _caption(tr("Select a filename"), caption), _path(path), filter);
    dlg->setFileMode(ExistingFile);
    dlg->setLabelText(QFileDialog::Accept, "Select");
    const QString fn(dlg->exec(path));
    delete dlg;
    return fn;
}

QStringList c2bFileDialog::getOpenFilenames(QWidget* parentw, const QString& caption, const QString& path, const QString& filter)
{
    c2bFileDialog* dlg = new c2bFileDialog(parentw, _caption(tr("Select filenames"), caption), _path(path), filter);
    dlg->setFileMode(ExistingFiles);
    dlg->setLabelText(QFileDialog::Accept, "Select");
    const QString fn(dlg->exec(path));
    QStringList fns;
    if (!fn.isEmpty())
        fns = dlg->selectedFiles();
    delete dlg;
    return fns;
}

QString c2bFileDialog::getSaveFilename(QWidget* parentw, const QString& caption, const QString& path, const QString& filter)
{
    c2bFileDialog* dlg = new c2bFileDialog(parentw, _caption(tr("Save As"), caption), _path(path), filter);
    dlg->setFileMode(AnyFile);
    dlg->setAcceptMode(AcceptSave);
    const QString fn(dlg->exec(path));
    delete dlg;
    return fn;
}

QString c2bFileDialog::getExistingDirectory(QWidget* parentw, const QString& caption, const QString& path)
{
    c2bFileDialog* dlg = new c2bFileDialog(parentw, _caption("Select directory", caption), _dirpath(path), QString());
    dlg->setFileMode(DirectoryOnly);
    dlg->setLabelText(QFileDialog::Accept, "Select");
    const QString fn(dlg->exec(QString()));
    delete dlg;
    return fn;
}

QString c2bFileDialog::getSystemFilename(QWidget* parentw, const QString& caption, const QString& path, const QString& filter)
{
    c2bFileDialog* dlg = new c2bFileDialog(parentw, _caption(tr("Select a filename"), caption), _path(path), filter);
    dlg->setFileMode(AnyFile);
    dlg->setLabelText(QFileDialog::Accept, "Select");
    dlg->setFilter(QDir::Drives | QDir::AllDirs | QDir::NoDotAndDotDot | QDir::System);
    dlg->setNameFilterDetailsVisible(false);
    const QString fn(dlg->exec(path));
    delete dlg;
    return fn;
}

QString c2bFileDialog::exec(const QString& path)
{
    readSettings();
    _current_file = path;
    const QStringList fnf(nameFilters().filter(QFileInfo(path).completeSuffix() + ')', Qt::CaseInsensitive));
    if (fnf.count() > 0)
        selectNameFilter(fnf.at(0));
    QTimer::singleShot(250, this, SLOT(selectCurrentFile()));
    QString sfn;
    if (QFileDialog::exec() == QDialog::Accepted)
    {
        const QStringList selected(selectedFiles());
        if (selected.count() > 0)
            sfn = selected.first();
    }
    writeSettings();
    return QDir::toNativeSeparators(QDir::cleanPath(sfn));
}

void c2bFileDialog::accept()
{
    // Set suffix for getSaveFilename dialog
    if (acceptMode() == AcceptSave)
    {
        QString suffix(selectedNameFilter());
        QRegExp sre("\\*\\.([\\w\\.]+)");
        if (sre.indexIn(suffix) != -1)
        {
            suffix = sre.cap(1);
            setDefaultSuffix(suffix);
        }
    }
    QFileDialog::accept();
}

void c2bFileDialog::selectCurrentFile()
{
    // Bug fix for Qt 4.4 not giving correct selection
    const QString fn(_filename(_current_file));
    QLineEdit* le = findChild<QLineEdit*>();
    if (le)
    {
        le->setText(fn);
        le->selectAll();
    }
}

void c2bFileDialog::readSettings()
{
    // Size
    resize(settings->value("c2bFileDialog/size", sizeHint()).toSize());

    // History
    QStringList _history(settings->value("c2bFileDialog/history").toStringList());
    _history.removeAll(directory().absolutePath());
    qSort(_history);
    setHistory(_history);
}

void c2bFileDialog::writeSettings()
{
    // Size
    settings->setValue("c2bFileDialog/size", size());

    // History
    if (result() == QDialog::Accepted)
    {
        // Unique paths, keeping the newest history entries
        QStringList _history(history());
        _history.append(directory().absolutePath());
        _history.removeAll(QDir::homePath());
        _history.removeAll(QDir::rootPath());
        QStringList chronological(settings->value("c2bFileDialog/history").toStringList());
        chronological.append(_history.last());
        // Discard repeated
        _history.clear();
        for (int i = chronological.count() - 1; i > -1; --i)
            if (!_history.contains(chronological.at(i)))
            {
                _history.prepend(chronological.at(i));
                if (_history.count() > 10)
                    break;
            }
        settings->setValue("c2bFileDialog/history", _history);
    }
}

QString c2bFileDialog::_caption(const QString& generic, const QString& caption)
{
    if (caption.isEmpty())
        return generic + " - cb2Bib";
    else
        return caption + " - cb2Bib";
}

QString c2bFileDialog::_filename(const QString& path)
{
    if (path.isEmpty())
        return path;
    else
        return QFileInfo(path).fileName();
}

QString c2bFileDialog::_path(const QString& path)
{
    if (path.isEmpty())
        return QDir::homePath();
    else
        return QFileInfo(path).absolutePath();
}

QString c2bFileDialog::_dirpath(const QString& path)
{
    if (path.isEmpty())
        return QDir::homePath();
    else
        return QFileInfo(path + '/').absolutePath();
}


/***************************************************************************
 Class fileIconProvider
 ***************************************************************************/

fileIconProvider::fileIconProvider() : QFileIconProvider()
{
    _exec_icon = QIcon(":/icons/icons/application-x-executable.png");
}

fileIconProvider::~fileIconProvider()
{}


QIcon fileIconProvider::icon(const QFileInfo&  info) const
{
    if (info.isExecutable() && !info.isDir())
        return _exec_icon;
    else
        return QFileIconProvider::icon(info);
}