File: xlineedit.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 (250 lines) | stat: -rw-r--r-- 6,137 bytes parent folder | download
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * 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 "xlineedit.h"

#include <QFocusEvent>
#include <QKeyEvent>
#include <QLocale>
#include <QMouseEvent>
#include <QValidator>

#include "format.h"

#define DEBUG false

XLineEdit::XLineEdit(QWidget *parent, const char *name) :
  QLineEdit(parent)
{
  if (! name && objectName().isEmpty())
    setObjectName("XLineEdit");
  setAcceptDrops(false);

#ifdef Q_OS_MAC
  QFont f = font();
  f.setPointSize(f.pointSize() - 2);
  setFont(f);
#endif

  _parsed = true;
  _valid = false;
  _isNull = true;

  _id = -1;
  connect(this, SIGNAL(editingFinished()), this, SLOT(sParse()));

  _listAct = new QAction(tr("List..."), this);
  _listAct->setShortcut(QKeySequence(tr("Ctrl+Shift+L")));
  _listAct->setShortcutContext(Qt::WidgetShortcut);
  _listAct->setToolTip(tr("List all records"));
  connect(_listAct, SIGNAL(triggered()), this, SIGNAL(requestList()));
  addAction(_listAct);

  _searchAct = new QAction(tr("Search..."), this);
  _searchAct->setShortcut(QKeySequence(tr("Ctrl+Shift+Space")));
  _searchAct->setShortcutContext(Qt::WidgetShortcut);
  _searchAct->setToolTip(tr("Search on specific criteria"));
  connect(_searchAct, SIGNAL(triggered()), this, SIGNAL(requestSearch()));
  addAction(_searchAct);

  _aliasAct = new QAction(tr("Alias..."), this);
  _aliasAct->setShortcut(QKeySequence(tr("Ctrl+Shift+A")));
  _aliasAct->setShortcutContext(Qt::WidgetShortcut);
  _aliasAct->setToolTip(tr("List of alias records"));
  connect(_aliasAct, SIGNAL(triggered()), this, SIGNAL(requestAlias()));
  addAction(_aliasAct);

  _mapper = new XDataWidgetMapper(this);
}

bool XLineEdit::isValid()
{
  sParse();
  return _valid;
}

int XLineEdit::id()
{
  sParse();
  return _id;
}

void XLineEdit::sParse()
{
  if (validator() && validator()->inherits("QDoubleValidator"))
  {
    QRegExp zeroRegex(QString("^[0") + QLocale().groupSeparator() + "]*" +
                      QLocale().decimalPoint() + "0*$");
    if (! text().isEmpty() && toDouble() == 0 && ! text().contains(zeroRegex))
    {
      _valid = false;
      setText("");
    }
    else _valid = true;
  }
  if (DEBUG)
    qDebug("%s::sParse() _valid = %d",
           objectName().isEmpty() ? "XLineEdit" : qPrintable(objectName()),
           _valid);
}

double XLineEdit::toDouble(bool *pIsValid)
{
  QString strippedText = text().remove(QLocale().groupSeparator());
  double result = QLocale().toDouble(strippedText, pIsValid);
  _valid = pIsValid;
  if (DEBUG)
    qDebug("%s::toDouble() returning %f (%d) for %s (%s)",
           objectName().isEmpty() ? "XLineEdit" : qPrintable(objectName()),
           result, _valid, qPrintable(text()), qPrintable(strippedText));
  return result;
}

void XLineEdit::setDataWidgetMap(XDataWidgetMapper* m)
{
  _mapper=m;
  m->addMapping(this, _fieldName, QByteArray("text"), QByteArray("defaultText"));
  connect(this, SIGNAL(editingFinished()), this, SLOT(setData()));
}

void XLineEdit::setText(const QVariant &pVariant)
{
  if (pVariant.type() == QVariant::Double)
  {
    const QValidator *v = validator();
    int prec = 0;

    if (v && v->inherits("QDoubleValidator"))
      prec = ((QDoubleValidator*)v)->decimals();
    else if (v && v->inherits("QIntValidator"))
      prec = 0;

    QLineEdit::setText(formatNumber(pVariant.toDouble(), prec));
  }
  else
    QLineEdit::setText(pVariant.toString());
  sHandleNullStr();
}


void XLineEdit::setDouble(const double pDouble, const int pPrec)
{
  const QValidator *v = validator();
  int prec = pPrec;

  if (pPrec < 0 && v && v->inherits("QDoubleValidator"))
    prec = ((QDoubleValidator*)v)->decimals();
  else if (pPrec < 0 && v && v->inherits("QIntValidator"))
    prec = 0;

  QLineEdit::setText(formatNumber(pDouble, prec));
}

void XLineEdit::mouseDoubleClickEvent(QMouseEvent *event)
{
  emit doubleClicked();

  QLineEdit::mouseDoubleClickEvent(event);
}

void XLineEdit::mousePressEvent(QMouseEvent *event)
{
  emit clicked();

  QLineEdit::mousePressEvent(event);
}

void XLineEdit::keyPressEvent(QKeyEvent *event)
{
  _parsed = false;
  QLineEdit::keyPressEvent(event);
}

void XLineEdit::setData()
{
  setData(text());
}

void XLineEdit::setData(const QString &text)
{
  if (_mapper->model())
    _mapper->model()->setData(_mapper->model()->index(_mapper->currentIndex(),_mapper->mappedSection(this)), text);
}

void XLineEdit::setNullStr(const QString &text)
{
  if (_nullStr == text )
    return;

  _nullStr = text;
  sHandleNullStr();
  if (!_nullStr.isEmpty())
    connect(this, SIGNAL(textChanged(QString)), this, SLOT(sHandleNullStr()));
}

void XLineEdit::sHandleNullStr()
{
  if (_nullStr.isEmpty())
    return;

  QString sheet = styleSheet();
  QString nullStyle = " QLineEdit{ color: Grey; "
                      "            font: italic; "
                      "            font-size: 12px}";

  if (!hasFocus() &&
      text().isEmpty())
  {
    setText(_nullStr);
    sheet.append(nullStyle);
    _isNull = true;
  }
  else if (hasFocus() &&
           sheet.contains(nullStyle))
  {
    clear();
    sheet.remove(nullStyle);
  }
  else
  {
    _isNull = false;
    sheet.remove(nullStyle);
  }

  setStyleSheet(sheet);
}

void XLineEdit::focusInEvent(QFocusEvent * event)
{
  if (!_nullStr.isEmpty())
  {
    disconnect(this, SIGNAL(textChanged(QString)), this, SLOT(sHandleNullStr()));
    sHandleNullStr();
  }
  QLineEdit::focusInEvent(event);
}

void XLineEdit::focusOutEvent(QFocusEvent * event)
{
  if (!_nullStr.isEmpty())
  {
    connect(this, SIGNAL(textChanged(QString)), this, SLOT(sHandleNullStr()));
    sHandleNullStr();
  }
  QLineEdit::focusOutEvent(event);
}

bool XLineEdit::isNull()
{
  if (_nullStr.isEmpty() || hasFocus())
    return text().isEmpty();
  return _isNull;
}