summaryrefslogtreecommitdiffstats
path: root/kmymoney2/dialogs/settings/ksettingshome.cpp
blob: 44bd10b7850e65137dbdf49018c5ba72cdc22504 (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
/***************************************************************************
                             ksettingshome.cpp
                             --------------------
    copyright            : (C) 2005 by Thomas Baumgart
    email                : ipwizard@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

#include <tqstringlist.h>
#include <tqheader.h>

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

#include <klistview.h>
#include <kpushbutton.h>
#include <kglobalsettings.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <klocale.h>
#include <ktextedit.h>

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

#include "ksettingshome.h"
#include "kmymoney2/kmymoneyglobalsettings.h"
#include "kmymoney2/kmymoneyutils.h"

KSettingsHome::KSettingsHome(TQWidget* parent, const char* name) :
  KSettingsHomeDecl(parent, name),
  m_noNeedToUpdateList(false)
{
  m_homePageList->addColumn("");
  m_homePageList->setSorting(-1);
  m_homePageList->header()->hide();
  m_homePageList->setAllColumnsShowFocus(true);

  KIconLoader* il = KGlobal::iconLoader();
  KGuiItem upButtonItem( i18n( "&Up" ),
                    TQIconSet(il->loadIcon("up", KIcon::Small, KIcon::SizeSmall)),
                    i18n("Move selected item up"),
                    i18n("Use this to move the selected item up by one position in the list."));
  KGuiItem downButtonItem( i18n( "&Down" ),
                    TQIconSet(il->loadIcon("down", KIcon::Small, KIcon::SizeSmall)),
                    i18n("Move selected item down"),
                    i18n("Use this to move the selected item down by one position in the list."));

  m_upButton->setGuiItem(upButtonItem);
  m_upButton->setEnabled(false);
  m_downButton->setGuiItem(downButtonItem);
  m_downButton->setEnabled(false);

  // connect this, so that the list gets loaded once the edit field is filled
  connect(kcfg_ItemList, TQT_SIGNAL(textChanged()), this, TQT_SLOT(slotLoadItems()));

  connect(m_homePageList, TQT_SIGNAL(selectionChanged(TQListViewItem*)),
          this, TQT_SLOT(slotSelectHomePageItem(TQListViewItem *)));
  connect(m_homePageList, TQT_SIGNAL(pressed(TQListViewItem*)), this, TQT_SLOT(slotUpdateItemList()));

  connect(m_upButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotMoveUp()));
  connect(m_downButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotMoveDown()));

  // Don't show it to the user, we only need it to load and save the settings
  kcfg_ItemList->hide();
}

KSettingsHome::~KSettingsHome()
{
}

void KSettingsHome::slotLoadItems(void)
{
  if(m_noNeedToUpdateList)
    return;

  TQStringList list = KMyMoneyGlobalSettings::itemList();
  TQStringList::ConstIterator it;
  int w = 0;
  m_homePageList->clear();
  TQCheckListItem *sel = 0;

  TQFontMetrics fm( KGlobalSettings::generalFont());
  TQCheckListItem* last = 0;

  for(it = list.begin(); it != list.end(); ++it) {
    int idx = (*it).toInt();
    // skip over unknown item entries
    if(idx == 0)
      continue;
    bool enabled = idx > 0;
    if(!enabled) idx = -idx;
    TQCheckListItem* item = new TQCheckListItem(m_homePageList, KMyMoneyUtils::homePageItemToString(idx), TQCheckListItem::CheckBox);
    if(last)
      item->moveItem(last);

    // qDebug("Adding %s", item->text(0).latin1());
    item->setOn(enabled);
    if(item->width(fm, m_homePageList, 0) > w)
      w = item->width(fm, m_homePageList, 0);

    if(sel == 0)
      sel = item;
    last = item;
  }

  if(sel) {
    m_homePageList->setSelected(sel, true);
    slotSelectHomePageItem(sel);
  }
}

void KSettingsHome::slotUpdateItemList(void)
{
  TQString list;
  TQListViewItem *it;

  for(it = m_homePageList->firstChild(); it; ) {
    int item = KMyMoneyUtils::stringToHomePageItem(it->text(0));
    if(!(static_cast<TQCheckListItem*>(it)->isOn()))
      item = -item;
    list += TQString::number(item);
    it = it->nextSibling();
    if(it)
      list += ",";
  }

  // don't update the list
  m_noNeedToUpdateList = true;
  kcfg_ItemList->setText(list);
  m_noNeedToUpdateList = false;
}

void KSettingsHome::slotSelectHomePageItem(TQListViewItem *item)
{
  m_upButton->setEnabled(m_homePageList->firstChild() != item);
  m_downButton->setEnabled(item->nextSibling());
}

void KSettingsHome::slotMoveUp(void)
{
  TQListViewItem *item = m_homePageList->currentItem();
  TQListViewItem *prev = item->itemAbove();
  if(prev) {
    prev->moveItem(item);
    slotSelectHomePageItem(item);
    slotUpdateItemList();
  }
}

void KSettingsHome::slotMoveDown(void)
{
  TQListViewItem *item = m_homePageList->currentItem();
  TQListViewItem *next = item->nextSibling();
  if(next) {
    item->moveItem(next);
    slotSelectHomePageItem(item);
    slotUpdateItemList();
  }
}

#include "ksettingshome.moc"