summaryrefslogtreecommitdiffstats
path: root/kmymoney2/widgets/transactionsortoption.ui.h
blob: cf87646aba32438c38dfde3e14f955a71ddf629c (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
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
/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you wish to add, delete or rename functions or slots use
** TQt Designer which will update this file, preserving your code. Create an
** init() function in place of a constructor, and a destroy() function in
** place of a destructor.
*****************************************************************************/

// ----------------------------------------------------------------------------
// QT Includes

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

#include <kiconloader.h>
#include <klocale.h>
#include <kpushbutton.h>

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

#include <kmymoney/register.h>
#include "sortoptionlistitem.h"


void TransactionSortOption::init()
{
  KIconLoader* il = TDEGlobal::iconLoader();
  m_addButton->setIconSet(TQIconSet(il->loadIcon("1rightarrow", KIcon::Small, KIcon::SizeSmall)));
  m_removeButton->setIconSet(TQIconSet(il->loadIcon("1leftarrow", KIcon::Small, KIcon::SizeSmall)));
  m_upButton->setIconSet(TQIconSet(il->loadIcon("1uparrow", KIcon::Small, KIcon::SizeSmall)));
  m_downButton->setIconSet(TQIconSet(il->loadIcon("1downarrow", KIcon::Small, KIcon::SizeSmall)));

  // don't allow sorting of the selected entries
  m_selectedList->setSortColumn(-1);

  // defaults to "post date, value" sorting
  // setSettings(TQString("1,4"));
  setSettings(TQString());

  TQListViewItem* p;
  if((p = m_availableList->firstChild()) != 0) {
    m_availableList->setSelected(p, true);
  }
}

/**
  * Setup the two lists according to the elements found in @a list.
  * If an item is negative, it will show up in the available list,
  * if positive, it shows up in the selected list.
  *
  * Special care is taken about the two values @a EntryDateSort and
  * @a EntryOrderSort. These two entries cannot (should not) exist
  * alone. Inside this widget, only the @a EntryOrderSort is used.
  *
  * setSettings() takes care of hiding the @a EntryDateSort item and if
  * it exists in @p settings without @a EntryOrderSort being present, it
  * will add @a EntryOrderSort.
  */
void TransactionSortOption::setSettings(const TQString& settings)
{
  m_availableList->clear();
  m_selectedList->clear();

  TQStringList list = TQStringList::split(',', settings);
  TQMap<int, bool> selectedMap;

  // fill selected list
  TQStringList::const_iterator it_s;
  TQListViewItem* last = 0;
  int dateSign = 1;
  for(it_s = list.begin(); it_s != list.end(); ++it_s) {
    int val = (*it_s).toInt();
    selectedMap[abs(val)] = true;
    // skip EntryDateSort but keep sign
    if(abs(val) == static_cast<int>(KMyMoneyRegister::EntryDateSort)) {
      dateSign = (val < 0) ? -1 : 1;
      continue;
    }
    last = addEntry(m_selectedList, last, val);
  }

  // make sure to create EntryOrderSort if missing but required
  if(selectedMap.find(static_cast<int>(KMyMoneyRegister::EntryDateSort)) != selectedMap.end()
  && selectedMap.find(static_cast<int>(KMyMoneyRegister::EntryOrderSort)) == selectedMap.end()) {
    int val = dateSign * static_cast<int>(KMyMoneyRegister::EntryOrderSort);
    selectedMap[static_cast<int>(KMyMoneyRegister::EntryOrderSort)] = true;
    last = addEntry(m_selectedList, last, val);
  }

  // fill available list
  TQMap<int, bool>::const_iterator it_m;
  for(int i = static_cast<int>(KMyMoneyRegister::PostDateSort);
      i < static_cast<int>(KMyMoneyRegister::MaxSortFields); ++i) {
    // Never add EntryDateSort
    if(i == static_cast<int>(KMyMoneyRegister::EntryDateSort))
      continue;
    // Only add those, that are not present in the list of selected items
    if(selectedMap.find(i) == selectedMap.end()) {
      int val = i;
      if(i == static_cast<int>(KMyMoneyRegister::ValueSort))
        val = -val;
      addEntry(m_availableList, 0, val);
    }
  }
}

TQListViewItem* TransactionSortOption::addEntry( KListView * p, TQListViewItem* after, int idx )
{
  TQString txt = KMyMoneyRegister::sortOrderToText(static_cast<KMyMoneyRegister::TransactionSortField>(abs(idx)));
  if(txt.isEmpty())
    txt = "Unknown";    // i18n should be handled in sortOptionToText()

  return new SortOptionListItem(p, after, txt, idx);
}

void TransactionSortOption::toggleDirection(TQListViewItem* item)
{
  SortOptionListItem* p = dynamic_cast<SortOptionListItem*>(item);
  if(p) {
    p->toggleDirection();
    emit settingsChanged(settings());
  }
}

TQString TransactionSortOption::settings( void ) const
{
  TQString rc;
  SortOptionListItem* item = dynamic_cast<SortOptionListItem*>(m_selectedList->firstChild());
  while(item) {
    int option = KMyMoneyRegister::textToSortOrder(item->text(0));
    // if we look at the EntryOrderSort option, we have to make
    // sure, that the EntryDateSort is prepended
    if(option == KMyMoneyRegister::EntryOrderSort) {
      rc  += TQString::number(static_cast<int>(KMyMoneyRegister::EntryDateSort)*item->direction())+",";
    }
    rc += TQString::number(KMyMoneyRegister::textToSortOrder(item->text(0))*item->direction());
    item = dynamic_cast<SortOptionListItem*>(item->itemBelow());
    if(item != 0)
      rc += ",";
  }
  return rc;
}

void TransactionSortOption::slotAvailableSelected( TQListViewItem * item )
{
  m_addButton->setEnabled(item != 0);
  m_removeButton->setDisabled(true);
  m_upButton->setDisabled(true);
  m_downButton->setDisabled(true);

  TQListViewItem* p = m_selectedList->currentItem();
  if(p) {
    m_selectedList->setSelected(p, false);
  }
}

void TransactionSortOption::slotSelectedSelected( TQListViewItem * item )
{
  m_addButton->setDisabled(true);
  m_removeButton->setEnabled(item != 0);
  if(item) {
    m_upButton->setEnabled(item->itemAbove() != 0);
    m_downButton->setEnabled(item->itemBelow() != 0);
  } else {
    m_upButton->setEnabled(false);
    m_downButton->setEnabled(false);
  }

  TQListViewItem* p = m_availableList->currentItem();
  if(p) {
    m_availableList->setSelected(p, false);
  }
}

void TransactionSortOption::slotAddItem( void )
{
  TQListViewItem* item;
  if((item = m_availableList->currentItem()) != 0) {
    TQListViewItem* next = item->itemBelow();
    if(!next)
      next = item->itemAbove();
    m_availableList->takeItem(item);
    m_selectedList->insertItem(item);
    m_addButton->setEnabled(m_availableList->firstChild() != 0);
    if(next) {
      m_availableList->setCurrentItem(next);
      m_availableList->setSelected(next, true);
    }
    emit settingsChanged(settings());
  }
}

void TransactionSortOption::slotRemoveItem( void )
{
  TQListViewItem* item;
  if((item = m_selectedList->currentItem()) != 0) {
    TQListViewItem* next = item->itemBelow();
    if(!next)
      next = item->itemAbove();
    m_selectedList->takeItem(item);
    m_availableList->insertItem(item);
    m_removeButton->setEnabled(m_selectedList->firstChild() != 0);
    if(next) {
      m_selectedList->setCurrentItem(next);
      m_selectedList->setSelected(next, true);
    }
    emit settingsChanged(settings());
  }
}

void TransactionSortOption::slotUpItem( void )
{
  TQListViewItem* item;
  if((item = m_selectedList->currentItem()) != 0) {
    TQListViewItem* prev = item->itemAbove();
    if(prev) {
      prev->moveItem(item);
      m_selectedList->setCurrentItem(item);
      m_selectedList->setSelected(item, true);
      m_upButton->setEnabled(item->itemAbove() != 0);
      m_downButton->setEnabled(item->itemBelow() != 0);
      emit settingsChanged(settings());
    }
  }
}

void TransactionSortOption::slotDownItem( void )
{
  TQListViewItem* item;
  if((item = m_selectedList->currentItem()) != 0) {
    TQListViewItem* next = item->itemBelow();
    if(next) {
      item->moveItem(next);
      m_selectedList->setCurrentItem(item);
      m_selectedList->setSelected(item, true);
      m_upButton->setEnabled(item->itemAbove() != 0);
      m_downButton->setEnabled(item->itemBelow() != 0);
      emit settingsChanged(settings());
    }
  }
}