Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 /*===========================================================================*\
43 * *
44 * $Revision: 21016 $ *
45 * $LastChangedBy: schultz $ *
46 * $Date: 2015-07-16 16:48:42 +0200 (Do, 16 Jul 2015) $ *
47 * *
48 \*===========================================================================*/
49 
50 /*
51  * FilePicker.cc
52  *
53  * Created on: Jan 8, 2013
54  * Author: ebke
55  */
56 
57 #include "FilePicker.hh"
58 
59 #include <QHBoxLayout>
60 #include <QMessageBox>
61 
62 FilePicker::FilePicker(QWidget *parent) :
63  QWidget(parent),
64  textBox_(new QComboBox),
65  browseButton_(new QPushButton(trUtf8("Browse..."))),
66  recent_files_ini_key_("GenericFilePickerRecents"),
67  dialog_type_(DT_OPEN_FILE),
68  overwrite_confirmed_by_user_(false) {
69 
70  init();
71 }
72 
73 FilePicker::FilePicker(QString recent_files_ini_key,
74  DialogType dialog_type,
75  QWidget *parent) :
76  QWidget(parent),
77  textBox_(new QComboBox),
78  browseButton_(new QPushButton(trUtf8("Browse..."))),
79  recent_files_ini_key_(recent_files_ini_key),
80  dialog_type_(dialog_type),
81  overwrite_confirmed_by_user_(false) {
82 
83  init();
84 }
85 
86 void FilePicker::init() {
87  /*
88  * Do layout stuff.
89  */
90  textBox_->setSizePolicy(QSizePolicy::MinimumExpanding,
91  textBox_->sizePolicy().verticalPolicy());
92  textBox_->setEditable(true);
93  textBox_->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
94 
95  QHBoxLayout *layout = new QHBoxLayout(this);
96  layout->setContentsMargins(0, 0, 0, 0);
97  layout->addWidget(textBox_);
98  layout->addWidget(browseButton_);
99 
100  /*
101  * Do functional stuff.
102  */
103  textBox_->insertItems(
104  0,
105  OpenFlipper::Options::getRecentItems(recent_files_ini_key()));
106  connect(browseButton_, SIGNAL( clicked() ), this, SLOT( slot_browse() ));
107  //editTextChanged(const QString &);
108  connect(textBox_, SIGNAL( editTextChanged(const QString &) ),
109  this, SLOT( path_changed() ));
110 }
111 
112 void FilePicker::set_recent_files_ini_key(QString value) {
113  recent_files_ini_key_ = value;
114  textBox_->clear();
115  textBox_->insertItems(
116  0,
117  OpenFlipper::Options::getRecentItems(recent_files_ini_key_));
118 }
119 
120 void FilePicker::path_changed() {
121  overwrite_confirmed_by_user_ = false;
122 }
123 
124 bool FilePicker::confirm_overwrite_if_necessary() {
125  if (overwrite_confirmed_by_user()) return true;
126  if (dialog_type() != DT_SAVE_FILE) return true;
127  if (!QFile::exists(currentFileName())) return true;
128 
129  const QString file_name = QFileInfo(currentFileName()).fileName();
130  const QMessageBox::StandardButton result =
131  QMessageBox::question(
132  this, trUtf8("Overwrite File?"),
133  trUtf8("The file \"%1\" exists. "
134  "Do you want to overwrite?")
135  .arg(file_name),
136  QMessageBox::Yes | QMessageBox::No,
137  QMessageBox::Yes);
138 
139  if (result == QMessageBox::Yes) {
140  overwrite_confirmed_by_user_ = true;
141  return true;
142  } else {
143  return false;
144  }
145 }
146 
147 void FilePicker::slot_browse() {
148  OpenFlipper::Options::obtainPathName(
149  textBox_,
150  dialog_title().toUtf8().constData(),
151  file_filter().toUtf8().constData(),
152  recent_files_ini_key().toUtf8().constData(),
153  static_cast<OpenFlipper::Options::DialogType>(dialog_type()));
154 
155  std::cout << "overwrite confirmed." << std::endl;
156  overwrite_confirmed_by_user_ = true;
157 }
158 
159 FilePicker::~FilePicker() {
160 }