summaryrefslogtreecommitdiffstats
path: root/kmymoney2/widgets/kguiutils.cpp
blob: 24bf3f6740746e6f9783f5df2085a63938f2bb29 (plain)
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
/***************************************************************************
                         kguiutils.cpp  -  description
                            -------------------
   begin                : Fri Jan 27 2006
   copyright            : (C) 2006 Tony Bloomfield
   email                : Tony Bloomfield <tonybloom@users.sourceforge.net>
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

// ----------------------------------------------------------------------------
// QT Includes
 // No need for TQDateEdit, TQSpinBox, etc., since these always return values

#include <tqcheckbox.h>
#include <tqlistbox.h>
#include <tqcombobox.h>
#include <tqlineedit.h>
#include <tqpushbutton.h>
#include <tqwidget.h>
#include <tqhbox.h>
#include <tqspinbox.h>

// ----------------------------------------------------------------------------
// KDE Includes

// ----------------------------------------------------------------------------
// Project Includes

#include "kguiutils.h"
#include "../kmymoneyglobalsettings.h"

 /**************************************************************************
  *                                                                        *
  * The MandatoryFieldGroup code is courtesy of                            *
  * Mark Summerfield in TQt Quarterly                                       *
  * http://doc.trolltech.com/qq/qq11-mandatoryfields.html                  *
  *                                                                        *
  * Enhanced by Thomas Baumgart to support the lineedit field of a         *
  * a TQComboBox.                                                           *
  *                                                                        *
  **************************************************************************/

void kMandatoryFieldGroup::add(TQWidget *widget)
{
  if (!widgets.contains(widget)) {
    if (widget->inherits(TQCHECKBOX_OBJECT_NAME_STRING))
      connect((TQCheckBox*)widget->qt_cast(TQCHECKBOX_OBJECT_NAME_STRING),
               TQT_SIGNAL(clicked()),
               this, TQT_SLOT(changed()));

    else if (widget->inherits(TQCOMBOBOX_OBJECT_NAME_STRING)) {
      TQComboBox* combo = (TQComboBox*)widget->qt_cast(TQCOMBOBOX_OBJECT_NAME_STRING);
      TQLineEdit* lineedit = combo->lineEdit();
      if(lineedit) {
        connect(lineedit, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(changed()));
      } else {
        connect(combo, TQT_SIGNAL(highlighted(int)), this, TQT_SLOT(changed()));
      }
    }

    else if (widget->inherits(TQLINEEDIT_OBJECT_NAME_STRING))
      connect((TQLineEdit*)widget->qt_cast(TQLINEEDIT_OBJECT_NAME_STRING),
               TQT_SIGNAL(textChanged(const TQString&)),
               this, TQT_SLOT(changed()));

    else if (widget->inherits(TQSPINBOX_OBJECT_NAME_STRING))
      connect((TQSpinBox*)widget->qt_cast(TQSPINBOX_OBJECT_NAME_STRING),
               TQT_SIGNAL(valueChanged(const TQString&)),
                      this, TQT_SLOT(changed()));

    else if (widget->inherits(TQLISTBOX_OBJECT_NAME_STRING))
      connect((TQListBox*)widget->qt_cast(TQLISTBOX_OBJECT_NAME_STRING),
               TQT_SIGNAL(selectionChanged()),
                      this, TQT_SLOT(changed()));

    else {
      tqWarning("MandatoryFieldGroup: unsupported class %s",
               widget->className());
      return;
    }

    widget->setPaletteBackgroundColor(KMyMoneyGlobalSettings::requiredFieldColor());
    widgets.append(widget);
    changed();
  }
}


void kMandatoryFieldGroup::remove(TQWidget *widget)
{
  widget->setPaletteBackgroundColor(widget->colorGroup().background());
  widgets.remove(widget);
  changed();
}


void kMandatoryFieldGroup::setOkButton(TQPushButton *button)
{
  if (okButton && okButton != button)
    okButton->setEnabled(true);
  okButton = button;
  changed();
}


void kMandatoryFieldGroup::changed(void)
{
  bool enable = true;
  TQValueList<TQWidget *>::ConstIterator i;
  for (i = widgets.begin(); i != widgets.end(); ++i) {
    TQWidget *widget = *i;
    // disabled widgets don't count
    if(!(widget->isEnabled())) {
      continue;
    }
    if (widget->inherits(TQCHECKBOX_OBJECT_NAME_STRING)) {
      if (((TQCheckBox*)widget->qt_cast(TQCHECKBOX_OBJECT_NAME_STRING))->state() == TQButton::NoChange) {
        enable = false;
        break;
      } else
        continue;
    }
    if (widget->inherits(TQCOMBOBOX_OBJECT_NAME_STRING)) {
      if (((TQComboBox*)widget->qt_cast(TQCOMBOBOX_OBJECT_NAME_STRING))->currentText().isEmpty()) {
        enable = false;
        break;
      } else
        continue;
    }
    if (widget->inherits(TQLINEEDIT_OBJECT_NAME_STRING)) {
      if (((TQLineEdit*)widget->qt_cast(TQLINEEDIT_OBJECT_NAME_STRING))->text().isEmpty()) {
        enable = false;
        break;
      } else
        continue;
    }
    if (widget->inherits(TQLISTBOX_OBJECT_NAME_STRING)) {
      if (((TQListBox*)widget->qt_cast(TQLISTBOX_OBJECT_NAME_STRING))->selectedItem() == 0) {
        enable = false;
        break;
      } else
        continue;
    }
  }

  if (okButton)
    okButton->setEnabled(enable);
  m_enabled = enable;

  emit stateChanged();
  emit stateChanged(enable);
}


void kMandatoryFieldGroup::clear(void)
{
  TQValueList<TQWidget *>::Iterator i;
  for (i = widgets.begin(); i != widgets.end(); ++i)
    (*i)->setPaletteBackgroundColor((*i)->colorGroup().background());
  widgets.clear();
  if (okButton) {
    okButton->setEnabled(true);
    okButton = 0;
    m_enabled = true;
  }
}


#include "kguiutils.moc"