Seamly2D
Code documentation
dialogdatetimeformats.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2017 Seamly, LLC *
4  * *
5  * https://github.com/fashionfreedom/seamly2d *
6  * *
7  ***************************************************************************
8  **
9  ** Seamly2D is free software: you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation, either version 3 of the License, or
12  ** (at your option) any later version.
13  **
14  ** Seamly2D 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
17  ** GNU General Public License for more details.
18  **
19  ** You should have received a copy of the GNU General Public License
20  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
21  **
22  **************************************************************************
23 
24  ************************************************************************
25  **
26  ** @file dialogdatetimeformats.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 19 8, 2017
29  **
30  ** @brief
31  ** @copyright
32  ** This source code is part of the Valentine project, a pattern making
33  ** program, whose allow create and modeling patterns of clothing.
34  ** Copyright (C) 2017 Seamly2D project
35  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
36  **
37  ** Seamly2D is free software: you can redistribute it and/or modify
38  ** it under the terms of the GNU General Public License as published by
39  ** the Free Software Foundation, either version 3 of the License, or
40  ** (at your option) any later version.
41  **
42  ** Seamly2D is distributed in the hope that it will be useful,
43  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
44  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45  ** GNU General Public License for more details.
46  **
47  ** You should have received a copy of the GNU General Public License
48  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
49  **
50  *************************************************************************/
51 
52 #include "dialogdatetimeformats.h"
53 #include "ui_dialogdatetimeformats.h"
54 
55 #ifdef Q_CC_MSVC
56  #include <ciso646>
57 #endif /* Q_CC_MSVC */
58 
59 //---------------------------------------------------------------------------------------------------------------------
60 DialogDateTimeFormats::DialogDateTimeFormats(const QDate &date, const QStringList &predefinedFormats,
61  const QStringList &userDefinedFormats, QWidget *parent)
62  : QDialog(parent),
63  ui(new Ui::DialogDateTimeFormats),
64  m_dateMode(true),
65  m_date(date),
66  m_time(),
67  m_predefined(predefinedFormats)
68 {
69  ui->setupUi(this);
70 
71  Init(predefinedFormats, userDefinedFormats);
72 }
73 
74 //---------------------------------------------------------------------------------------------------------------------
75 DialogDateTimeFormats::DialogDateTimeFormats(const QTime &time, const QStringList &predefinedFormats,
76  const QStringList &userDefinedFormats, QWidget *parent)
77  : QDialog(parent),
78  ui(new Ui::DialogDateTimeFormats),
79  m_dateMode(false),
80  m_date(),
81  m_time(time),
82  m_predefined(predefinedFormats)
83 {
84  ui->setupUi(this);
85 
86  Init(predefinedFormats, userDefinedFormats);
87 }
88 
89 //---------------------------------------------------------------------------------------------------------------------
91 {
92  delete ui;
93 }
94 
95 //---------------------------------------------------------------------------------------------------------------------
97 {
98  QStringList formats;
99 
100  for (int i=0; i<ui->listWidget->count(); ++i)
101  {
102  if (const QListWidgetItem *lineItem = ui->listWidget->item(i))
103  {
104  const QString format = lineItem->data(Qt::UserRole).toString();
105  if (not format.isEmpty())
106  {
107  formats.append(lineItem->data(Qt::UserRole).toString());
108  }
109  }
110  }
111 
112  return formats;
113 }
114 
115 //---------------------------------------------------------------------------------------------------------------------
117 {
118  int row = ui->listWidget->count();
119  ui->listWidget->insertItem(++row, new QListWidgetItem(tr("<empty>")));
120  ui->listWidget->setCurrentRow(row);
121 }
122 
123 //---------------------------------------------------------------------------------------------------------------------
125 {
126  ui->listWidget->blockSignals(true);
127  if (QListWidgetItem *curLine = ui->listWidget->currentItem())
128  {
129  if (not m_predefined.contains(curLine->data(Qt::UserRole).toString()))
130  {
131  delete ui->listWidget->takeItem(ui->listWidget->currentRow());
132  }
133  }
134  ui->listWidget->blockSignals(false);
136 }
137 
138 //---------------------------------------------------------------------------------------------------------------------
139 void DialogDateTimeFormats::SaveFormat(const QString &text)
140 {
141  if (QListWidgetItem *curLine = ui->listWidget->currentItem())
142  {
143  if (not GetFormats().contains(text))
144  {
145  const QString preview = m_dateMode ? m_date.toString(text) : m_time.toString(text);
146  curLine->setText(preview);
147  curLine->setData(Qt::UserRole, text);
148  }
149  }
150 }
151 
152 //---------------------------------------------------------------------------------------------------------------------
154 {
155  if (ui->listWidget->count() > 0)
156  {
157  if (const QListWidgetItem *line = ui->listWidget->currentItem())
158  {
159  ui->lineEditFormat->blockSignals(true);
160  ui->lineEditFormat->setText(line->data(Qt::UserRole).toString());
161  ui->lineEditFormat->blockSignals(false);
162  }
163  }
164 
165  SetupControls();
166 }
167 
168 //---------------------------------------------------------------------------------------------------------------------
169 void DialogDateTimeFormats::Init(const QStringList &predefined, const QStringList &userDefined)
170 {
171  SetFormatLines(predefined, userDefined);
172 
173  ui->lineEditFormat->setClearButtonEnabled(true);
174 
175  connect(ui->toolButtonRemove, &QToolButton::clicked, this, &DialogDateTimeFormats::RemoveLine);
176  connect(ui->toolButtonAdd, &QToolButton::clicked, this, &DialogDateTimeFormats::AddLine);
177  connect(ui->lineEditFormat, &QLineEdit::textEdited, this, &DialogDateTimeFormats::SaveFormat);
178  connect(ui->listWidget, &QListWidget::itemSelectionChanged, this, &DialogDateTimeFormats::ShowFormatDetails);
179 
180  ui->listWidget->setCurrentRow(0);
181 }
182 
183 //---------------------------------------------------------------------------------------------------------------------
184 void DialogDateTimeFormats::SetFormatLines(const QStringList &predefined, const QStringList &userDefined)
185 {
186  ui->listWidget->blockSignals(true);
187  ui->listWidget->clear();
188 
189  int row = -1;
190 
191  for (int i=0; i<predefined.size(); ++i)
192  {
193  ui->listWidget->insertItem(++row, AddListLine(predefined.at(i)));
194  }
195 
196  for (int i=0; i<userDefined.size(); ++i)
197  {
198  ui->listWidget->insertItem(++row, AddListLine(userDefined.at(i)));
199  }
200 
201  ui->listWidget->blockSignals(false);
202 
203  if (ui->listWidget->count() > 0)
204  {
205  ui->listWidget->setCurrentRow(0);
206  }
207 }
208 
209 //---------------------------------------------------------------------------------------------------------------------
211 {
212  const bool enabled = ui->listWidget->count() > 0;
213 
214  if (not enabled)
215  {
216  ui->lineEditFormat->blockSignals(true);
217  ui->lineEditFormat->clear();
218  ui->lineEditFormat->blockSignals(false);
219  }
220 
221  ui->toolButtonAdd->setEnabled(true);
222 
223  ui->lineEditFormat->setEnabled(enabled);
224 
225  const QListWidgetItem *line = ui->listWidget->currentItem();
226  if (line != nullptr && m_predefined.contains(line->data(Qt::UserRole).toString()))
227  {
228  ui->toolButtonRemove->setEnabled(false);
229  ui->lineEditFormat->setReadOnly(true);
230  }
231  else
232  {
233  ui->toolButtonRemove->setEnabled(enabled);
234  ui->lineEditFormat->setReadOnly(false);
235  }
236 }
237 
238 //---------------------------------------------------------------------------------------------------------------------
239 QListWidgetItem *DialogDateTimeFormats::AddListLine(const QString &format)
240 {
241  const QString preview = m_dateMode ? m_date.toString(format) : m_time.toString(format);
242  QListWidgetItem *item = new QListWidgetItem(preview);
243  item->setData(Qt::UserRole, format);
244  return item;
245 }
void Init(const QStringList &predefined, const QStringList &userDefined)
void SetFormatLines(const QStringList &predefined, const QStringList &userDefined)
Ui::DialogDateTimeFormats * ui
QListWidgetItem * AddListLine(const QString &format)
DialogDateTimeFormats(const QDate &date, const QStringList &predefinedFormats, const QStringList &userDefinedFormats, QWidget *parent=nullptr)
QStringList GetFormats() const
void SaveFormat(const QString &text)