File: filemoveselector.cpp

package info (click to toggle)
postbooks 4.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 112,660 kB
  • ctags: 22,890
  • sloc: cpp: 310,358; sh: 607; xml: 214; python: 140; awk: 104; makefile: 50
file content (234 lines) | stat: -rw-r--r-- 7,686 bytes parent folder | download | duplicates (2)
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
/*
 * This file is part of the xTuple ERP: PostBooks Edition, a free and
 * open source Enterprise Resource Planning software suite,
 * Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
 * It is licensed to you under the Common Public Attribution License
 * version 1.0, the full text of which (including xTuple-specific Exhibits)
 * is available at www.xtuple.com/CPAL.  By using this software, you agree
 * to be bound by its terms.
 */

#include "filemoveselector.h"
#include "shortcuts.h"

#include <QtScript>

#define DEBUG false

/** \class FileMoveSelector

    \brief The FileMoveSelector provides a standardized user interface for
           selecting what the application should do with a file in the
           host file system after it has been processed.

    The FileMoveSelector encodes a simple user interface paradigm for
    how the application should handle files after they have been processed.
    The application window that has a FileMoveSelector must store the user's
    selection. The application code that processes the files should check
    this stored selection and have its own means of handling files in the
    selected manner.

    \see configureIE
    \see ImportHelper
 */

FileMoveSelector::FileMoveSelector(QWidget* parent, Qt::WindowFlags fl)
  : QWidget(parent, fl)
{
  setupUi(this);

  setObjectName("fileMoveSelector");

  shortcuts::setStandardKeys(this);

  _status->append(Nothing,   tr("Do nothing"),             codeForOption(Nothing));
  _status->append(Suffix,    tr("Add a suffix"),           codeForOption(Suffix));
  _status->append(ChangeDir, tr("Move to a subdirectory"), codeForOption(ChangeDir));
  _status->append(Delete,    tr("Delete"),                 codeForOption(Delete));
}

FileMoveSelector::~FileMoveSelector()
{
  // no need to delete child widgets, Qt does it all for us
}

void FileMoveSelector::languageChange()
{
  retranslateUi(this);
}

/** \brief Return the text code value of the currently-selected return option.

    \see codeForOption(FileMoveOption) for possible return values
  */
QString FileMoveSelector::code() const
{
  return _status->code();
}

/** \brief Translate a FileMoveOption enumerated value to a text code value.

  The text code values are not translatable. They are intended to be used
  for storing the user's selection in a database or configuration file, and
  for applications to read from the database or configuration file
  when deciding how to handle a file after it has been processed.

  \return The text code value corresponding to the FileMoveOption enumerated
          value.
          Possible values are "None", "Rename", "Move", and "Delete",
          or QString::null if poption is invalid or not yet supported.
  */
QString FileMoveSelector::codeForOption(FileMoveOption poption) const
{
  QString returnValue = QString::null;
  switch (poption)
  {
    case Nothing:       returnValue = "None";   break;
    case Suffix:        returnValue = "Rename"; break;
    case ChangeDir:     returnValue = "Move";   break;
    case Delete:        returnValue = "Delete"; break;
  }

  if (DEBUG)
    qDebug("%s::codeForOption(%d) returning '%s'",
           qPrintable(objectName()), poption, qPrintable(returnValue));
  return returnValue;
}

/** \brief The destination directory for files which are to be moved.

  This value reflects the current contents of the destination directory
  field. The destination directory may not be visible, depending on the
  currently selected option.

  \returns The text entered in the destination directory field of the
           FileMoveSelector. This value should not be used to determine
           how the user would like files to be handled. Use code() or
           option() for this purpose.

  */
QString FileMoveSelector::destdir() const
{
  return _movedir->text();
}

/** \brief The enumerated FileMoveOption value of the current selection.

  \returns The currently selected option, or -1 if there is an error.
  */
FileMoveSelector::FileMoveOption FileMoveSelector::option() const
{
  return (FileMoveOption)_status->id();
}

/** \brief The suffix that should be appended to files that have been processed.

  This value reflects the current contents of the suffix field. This field
  may not be visible, depending on the currently selected option.

  \returns The text entered in the suffix field of the FileMoveSelector.
           This value should not be used to determine how the user would
           like files to be handled. Use code() or option() for this purpose.
  */
QString FileMoveSelector::suffix() const
{
  return _suffix->text();
}

/** \brief Programatically set the selection using a text code value.

    \return true if successful or false if the pcode does not exactly match
            a known value.
  */
bool FileMoveSelector::setCode(QString pcode)
{
  if (DEBUG)
    qDebug("%s::setCode(%s) entered",
           qPrintable(objectName()), qPrintable(pcode));
  _status->setCode(pcode);
  if (_status->id() < 0)
    _status->setItemText(0, _status->nullStr());

  if (DEBUG)
    qDebug("%s::setCode() returning %d with id %d",
           qPrintable(objectName()), _status->id() >= 0, _status->id());
  return (_status->id() >= 0);
}

/** \brief Programatically set the destination directory.

    This does not automatically set the selection to ChangeDir.

    \return true
  */
bool FileMoveSelector::setDestdir(QString pdir)
{
  _movedir->setText(pdir);
  return true;
}

/** \brief Programatically set the selection using one of the
           FileMoveOption enumerated values.

    \return true if successful or false if poption is not valid.
  */
bool FileMoveSelector::setOption(FileMoveOption poption)
{
  _status->setId(poption);
  return (_status->id() >= 0);
}

/** \brief Programatically set the suffix to append.

    This does not automatically set the selection to Suffix.

    \return true
  */
bool FileMoveSelector::setSuffix(QString psuffix)
{
  _suffix->setText(psuffix);
  return true;
}

/* script exposure *********************************************************/

QScriptValue constructFileMoveSelector(QScriptContext *context,
                                       QScriptEngine  *engine)
{
  FileMoveSelector *obj = 0;

  if (context->argumentCount() == 0)
    obj = new FileMoveSelector();
  else if (context->argumentCount() == 1 &&
           qscriptvalue_cast<QWidget*>(context->argument(0)))
    obj = new FileMoveSelector(qscriptvalue_cast<QWidget*>(context->argument(0)));
  else if (context->argumentCount() > 1 &&
           qscriptvalue_cast<QWidget*>(context->argument(0))
           && context->argument(1).isNumber())
    obj = new FileMoveSelector(qscriptvalue_cast<QWidget*>(context->argument(0)),
                               (Qt::WindowFlags)context->argument(1).toInt32());
  else
     context->throwError(QScriptContext::UnknownError,
                         "Could not find an appropriate FileMoveSelector constructor");

  return engine->toScriptValue(obj);
}

QScriptValue FileMoveSelectorToScriptValue(QScriptEngine *engine, FileMoveSelector *const &item)
{
  return engine->newQObject(item);
}

void FileMoveSelectorFromScriptValue(const QScriptValue &obj, FileMoveSelector * &item)
{
  item = qobject_cast<FileMoveSelector*>(obj.toQObject());
}

void setupFileMoveSelector(QScriptEngine *engine)
{
  qScriptRegisterMetaType(engine, FileMoveSelectorToScriptValue, FileMoveSelectorFromScriptValue);

  QScriptValue constructor = engine->newFunction(constructFileMoveSelector);
  engine->globalObject().setProperty("FileMoveSelector", constructor,
                                     QScriptValue::ReadOnly | QScriptValue::Undeletable);
}