Seamly2D
Code documentation
vfileproperty.cpp
Go to the documentation of this file.
1 /************************************************************************
2  **
3  ** @file vfileproperty.cpp
4  ** @author hedgeware <internal(at)hedgeware.net>
5  ** @date
6  **
7  ** @brief
8  ** @copyright
9  ** All rights reserved. This program and the accompanying materials
10  ** are made available under the terms of the GNU Lesser General Public License
11  ** (LGPL) version 2.1 which accompanies this distribution, and is available at
12  ** http://www.gnu.org/licenses/lgpl-2.1.html
13  **
14  ** This library is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  ** Lesser General Public License for more details.
18  **
19  *************************************************************************/
20 
21 #include "vfileproperty.h"
22 
23 #include <QAbstractItemDelegate>
24 #include <QFileInfo>
25 #include <QLocale>
26 #include <QWidget>
27 
28 #include "../vfileproperty_p.h"
29 #include "vfilepropertyeditor.h"
30 #include "../vproperty_p.h"
31 
33  : VProperty(new VFilePropertyPrivate(name, QVariant::String))
34 {
35 
36 }
37 
39 {
40  //
41 }
42 
43 
44 void VPE::VFileProperty::setFileFilters(const QString& filefilters)
45 {
46  static_cast<VFilePropertyPrivate*>(d_ptr)->FileFilters = filefilters;
47 }
48 
49 
51 {
52  return static_cast<VFilePropertyPrivate*>(d_ptr)->FileFilters;
53 }
54 
55 
56 void VPE::VFileProperty::setFile(const QString& file)
57 {
58  d_ptr->VariantValue.setValue(file);
59 }
60 
61 
63 {
64  return d_ptr->VariantValue.toString();
65 }
66 
67 
68 QVariant VPE::VFileProperty::data (int column, int role) const
69 {
70  if (column == DPC_Data && (Qt::DisplayRole == role || Qt::EditRole == role))
71  {
72  QFileInfo tmpFile(d_ptr->VariantValue.toString());
73  return tmpFile.fileName();
74  }
75  else
76  return VProperty::data(column, role);
77 }
78 
79 
80 QWidget* VPE::VFileProperty::createEditor(QWidget * parent, const QStyleOptionViewItem& options,
81  const QAbstractItemDelegate* delegate)
82 {
83  Q_UNUSED(options)
84 
85  VFileEditWidget* tmpWidget = new VFileEditWidget(parent);
86  if (delegate)
87  {
88  VFileEditWidget::connect(tmpWidget, SIGNAL(commitData(QWidget*)), delegate, SIGNAL(commitData(QWidget*)));
89 
90  }
91  tmpWidget->setLocale(parent->locale());
92  tmpWidget->setFilter(static_cast<VFilePropertyPrivate*>(d_ptr)->FileFilters); // todo: parse this string
93  tmpWidget->setFile(d_ptr->VariantValue.toString());
94  tmpWidget->setDirectory(static_cast<VFilePropertyPrivate*>(d_ptr)->Directory);
95  return tmpWidget;
96 }
97 
98 
99 bool VPE::VFileProperty::setEditorData(QWidget* editor)
100 {
101  VFileEditWidget* tmpWidget = qobject_cast<VFileEditWidget*>(editor);
102  if (tmpWidget)
103  {
104  tmpWidget->setFile(d_ptr->VariantValue.toString());
105  }
106  else
107  return false;
108 
109  return true;
110 }
111 
112 
113 QVariant VPE::VFileProperty::getEditorData(const QWidget *editor) const
114 {
115  const VFileEditWidget* tmpWidget = qobject_cast<const VFileEditWidget*>(editor);
116  if (tmpWidget)
117  {
118  return tmpWidget->getFile();
119  }
120 
121  return QVariant();
122 }
123 
124 void VPE::VFileProperty::setSetting(const QString& key, const QVariant& value)
125 {
126  if (key == "FileFilters")
127  {
128  setFileFilters(value.toString());
129  }
130  else if (key == "Directory")
131  {
132  setDirectory(value.toBool());
133  }
134 }
135 
136 QVariant VPE::VFileProperty::getSetting(const QString& key) const
137 {
138  if (key == "FileFilters")
139  {
140  return getFileFilters();
141  }
142  else if (key == "Directory")
143  {
144  return isDirectory();
145  }
146  else
147  return VProperty::getSetting(key);
148 }
149 
151 {
152  return QStringList("FileFilters") << "Directory";
153 }
154 
156 {
157  return "file";
158 }
159 
160 VPE::VProperty* VPE::VFileProperty::clone(bool include_children, VProperty* container) const
161 {
162  return VProperty::clone(include_children, container ? container : new VFileProperty(getName()));
163 }
164 
166 {
167  return static_cast<VFilePropertyPrivate*>(d_ptr)->Directory;
168 }
169 
170 
171 void VPE::VFileProperty::setDirectory(bool is_directory)
172 {
173  static_cast<VFilePropertyPrivate*>(d_ptr)->Directory = is_directory;
174 }
void setDirectory(bool dir)
Sets whether the property stores a directory or a file.
void setFile(const QString &value, bool emit_signal=false)
Sets the current file, does not check if it is valid.
QString getFile() const
This function returns the file currently set to this editor.
void setFilter(const QString &dialog_filter=QString(), const QStringList &filter_list=QStringList())
Sets a filter for the file field.
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &options, const QAbstractItemDelegate *delegate) Q_DECL_OVERRIDE
Returns an editor widget, or NULL if it doesn't supply one.
virtual QString getFileFilters() const
Returns the current file filters as a string.
virtual bool isDirectory() const
Returns whether this is a file (false) or a directory (true)
virtual QString type() const Q_DECL_OVERRIDE
Returns a string containing the type of the property.
virtual Q_REQUIRED_RESULT VProperty * clone(bool include_children=true, VProperty *container=nullptr) const Q_DECL_OVERRIDE
Clones this property.
virtual ~VFileProperty() Q_DECL_OVERRIDE
The destructor.
VFileProperty(const QString &name)
virtual QVariant data(int column=DPC_Name, int role=Qt::DisplayRole) const Q_DECL_OVERRIDE
Get the data how it should be displayed.
virtual QVariant getSetting(const QString &key) const Q_DECL_OVERRIDE
Get the settings. This function has to be implemented in a subclass in order to have an effect.
virtual void setDirectory(bool is_directory)
Sets whether this is a file (false) or a directory (true)
virtual bool setEditorData(QWidget *editor) Q_DECL_OVERRIDE
Sets the property's data to the editor (returns false, if the standard delegate should do that)
virtual void setFile(const QString &file)
Set file.
virtual QVariant getEditorData(const QWidget *editor) const Q_DECL_OVERRIDE
Gets the data from the widget.
virtual void setSetting(const QString &key, const QVariant &value) Q_DECL_OVERRIDE
Sets the settings. Available settings:
virtual QStringList getSettingKeys() const Q_DECL_OVERRIDE
Returns the list of keys of the property's settings.
virtual void setFileFilters(const QString &filefilters)
Sets the file filters. The file filters have to be like the ones passed a QFileOpenDialog.
virtual QString getFile() const
Get file.
virtual Q_REQUIRED_RESULT VProperty * clone(bool include_children=true, VProperty *container=nullptr) const
Clones this property.
Definition: vproperty.cpp:372
virtual QVariant data(int column=DPC_Name, int role=Qt::DisplayRole) const
Get the data how it should be displayed.
Definition: vproperty.cpp:68
virtual QVariant getSetting(const QString &key) const
Get the settings. This function has to be implemented in a subclass in order to have an effect.
Definition: vproperty.cpp:360