Developer Documentation
FilePicker.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 #include "FilePicker.hh"
43 
44 #include <QHBoxLayout>
45 #include <QMessageBox>
46 
47 FilePicker::FilePicker(QWidget *parent) :
48  QWidget(parent),
49  textBox_(new QComboBox),
50  browseButton_(new QPushButton(trUtf8("Browse..."))),
51  recent_files_ini_key_("GenericFilePickerRecents"),
52  dialog_type_(DT_OPEN_FILE),
53  overwrite_confirmed_by_user_(false) {
54 
55  init();
56 }
57 
58 FilePicker::FilePicker(QString recent_files_ini_key,
59  DialogType dialog_type,
60  QWidget *parent) :
61  QWidget(parent),
62  textBox_(new QComboBox),
63  browseButton_(new QPushButton(trUtf8("Browse..."))),
64  recent_files_ini_key_(recent_files_ini_key),
65  dialog_type_(dialog_type),
66  overwrite_confirmed_by_user_(false) {
67 
68  init();
69 }
70 
71 void FilePicker::init() {
72  /*
73  * Do layout stuff.
74  */
75  textBox_->setSizePolicy(QSizePolicy::MinimumExpanding,
76  textBox_->sizePolicy().verticalPolicy());
77  textBox_->setEditable(true);
78  textBox_->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
79 
80  QHBoxLayout *layout = new QHBoxLayout(this);
81  layout->setContentsMargins(0, 0, 0, 0);
82  layout->addWidget(textBox_);
83  layout->addWidget(browseButton_);
84 
85  /*
86  * Do functional stuff.
87  */
88  textBox_->insertItems(
89  0,
90  OpenFlipper::Options::getRecentItems(recent_files_ini_key()));
91  connect(browseButton_, SIGNAL( clicked() ), this, SLOT( slot_browse() ));
92  //editTextChanged(const QString &);
93  connect(textBox_, SIGNAL( editTextChanged(const QString &) ),
94  this, SLOT( path_changed() ));
95 }
96 
97 void FilePicker::set_recent_files_ini_key(QString value) {
98  recent_files_ini_key_ = value;
99  textBox_->clear();
100  textBox_->insertItems(
101  0,
102  OpenFlipper::Options::getRecentItems(recent_files_ini_key_));
103 }
104 
105 void FilePicker::path_changed() {
106  overwrite_confirmed_by_user_ = false;
107 }
108 
109 bool FilePicker::confirm_overwrite_if_necessary() {
110  if (overwrite_confirmed_by_user()) return true;
111  if (dialog_type() != DT_SAVE_FILE) return true;
112  if (!QFile::exists(currentFileName())) return true;
113 
114  const QString file_name = QFileInfo(currentFileName()).fileName();
115  const QMessageBox::StandardButton result =
116  QMessageBox::question(
117  this, trUtf8("Overwrite File?"),
118  trUtf8("The file \"%1\" exists. "
119  "Do you want to overwrite?")
120  .arg(file_name),
121  QMessageBox::Yes | QMessageBox::No,
122  QMessageBox::Yes);
123 
124  if (result == QMessageBox::Yes) {
125  overwrite_confirmed_by_user_ = true;
126  return true;
127  } else {
128  return false;
129  }
130 }
131 
132 void FilePicker::slot_browse() {
133  OpenFlipper::Options::obtainPathName(
134  textBox_,
135  dialog_title().toUtf8().constData(),
136  file_filter().toUtf8().constData(),
137  recent_files_ini_key().toUtf8().constData(),
138  static_cast<OpenFlipper::Options::DialogType>(dialog_type()));
139 
140  std::cout << "overwrite confirmed." << std::endl;
141  overwrite_confirmed_by_user_ = true;
142 }
143 
144 FilePicker::~FilePicker() {
145 }