20#include "kptaccountspanel.h"
21#include "kptaccount.h"
22#include "kptcommand.h"
23#include "kptproject.h"
25#include <tqcombobox.h>
27#include <tqlistview.h>
28#include <tqpushbutton.h>
30#include <tqstringlist.h>
32#include <tdelistview.h>
40class AccountItem :
public TDEListViewItem {
42 AccountItem(AccountsPanel &pan, TQListView *parent)
43 : TDEListViewItem(parent), account(0), panel(pan)
45 AccountItem(AccountsPanel &pan, TQListViewItem *parent)
46 : TDEListViewItem(parent), account(0), panel(pan)
48 AccountItem(AccountsPanel &pan, TQListView *parent, TQString label1, TQString label2 = TQString())
49 : TDEListViewItem(parent, label1, label2), account(0), panel(pan)
51 AccountItem(AccountsPanel &pan, TQListViewItem *parent, TQString label1, TQString label2 = TQString())
52 : TDEListViewItem(parent, label1, label2), account(0), panel(pan)
54 AccountItem(AccountsPanel &pan, TQListView *parent, TQListViewItem *after)
55 : TDEListViewItem(parent, after), account(0), panel(pan)
57 AccountItem(AccountsPanel &pan, TQListViewItem *parent, TQListViewItem *after)
58 : TDEListViewItem(parent, after), account(0), panel(pan)
67 virtual void cancelRename(
int col) {
69 if ((col == 0 && oldText.isEmpty()) ||
70 (!panel.isUnique(
this))) {
73 panel.renameStopped(
this);
74 TQListViewItem::cancelRename(col);
75 setRenameEnabled(col,
false);
79 setRenameEnabled(0,
false);
80 setRenameEnabled(1,
false);
87AccountsPanel::AccountsPanel(
Accounts &acc, TQWidget *p,
const char *n)
88 : AccountsPanelBase(p, n),
94 accountList->setRootIsDecorated(
true);
95 accountList->header()->setStretchEnabled(
true, 1);
96 accountList->setItemMargin(2);
97 accountList->setDefaultRenameAction(TQListView::Accept);
98 addItems(accountList, acc);
100 slotSelectionChanged();
102 connect(accountList, TQ_SIGNAL(selectionChanged()), TQ_SLOT(slotSelectionChanged()));
103 connect(accountList, TQ_SIGNAL(itemRenamed(TQListViewItem*,
int)), TQ_SLOT(slotItemRenamed(TQListViewItem*,
int)));
104 connect(accountList, TQ_SIGNAL(doubleClicked(TQListViewItem*,
const TQPoint &,
int)), TQ_SLOT(slotListDoubleClicked(TQListViewItem*,
const TQPoint &,
int)));
106 connect(removeBtn, TQ_SIGNAL(clicked()), TQ_SLOT(slotRemoveBtn()));
107 connect(newBtn, TQ_SIGNAL(clicked()), TQ_SLOT(slotNewBtn()));
108 connect(subBtn, TQ_SIGNAL(clicked()), TQ_SLOT(slotSubBtn()));
110 connect(accountsComboBox, TQ_SIGNAL(activated(
int)), TQ_SLOT(slotActivated(
int)));
114 connect(
this, TQ_SIGNAL(renameStarted(TQListViewItem*,
int)), TQ_SLOT(slotRenameStarted(TQListViewItem*,
int)));
115 connect(
this, TQ_SIGNAL(startRename(TQListViewItem*,
int)), TQ_SLOT(slotStartRename(TQListViewItem*,
int)));
116 connect(
this, TQ_SIGNAL(selectionChanged()), TQ_SLOT(slotSelectionChanged()));
119void AccountsPanel::addItems(TQListView *lv, Accounts &acc) {
121 AccountListIterator it = acc.accountList();
122 for (; it.current(); ++it) {
123 TQString n = it.current()->name();
124 TQString d = it.current()->description();
125 AccountItem *item =
new AccountItem(*
this, lv, n, d);
126 item->account = it.current();
127 item->isDefault = (it.current() == acc.defaultAccount());
128 if (it.current()->isElement()) {
131 addItems(item, it.current());
135void AccountsPanel::addItems(TQListViewItem *item, Account *acc) {
136 AccountListIterator it = acc->accountList();
137 for (; it.current(); ++it) {
138 TQString n = it.current()->name();
139 TQString d = it.current()->description();
140 AccountItem *ai =
new AccountItem(*
this, item, n, d);
141 ai->account = it.current();
142 ai->isDefault = (it.current() == acc->list()->defaultAccount());
143 if (it.current()->isElement()) {
146 addItems(ai, it.current());
150void AccountsPanel::addElement(
const TQListViewItem *item) {
151 if (item->parent()) {
152 removeElement(item->parent());
154 m_elements.replace(item->text(0), item);
156 refreshDefaultAccount();
159void AccountsPanel::removeElement(TQListViewItem *item) {
160 static_cast<AccountItem*
>(item)->isDefault =
false;
161 m_elements.remove(item->text(0));
162 refreshDefaultAccount();
165void AccountsPanel::refreshDefaultAccount() {
166 accountsComboBox->clear();
168 accountsComboBox->insertItem(i18n(
"None"));
169 TQDictIterator<TQListViewItem> it(m_elements);
170 for(
int i=1; it.current(); ++it, ++i) {
171 accountsComboBox->insertItem(it.currentKey());
172 if (
static_cast<AccountItem*
>(it.current())->isDefault) {
174 accountsComboBox->setCurrentItem(i);
180void AccountsPanel::slotActivated(
int index) {
182 if (m_currentIndex >= (
int)m_elements.count()) {
183 kdError()<<k_funcinfo<<
"currentIndex ("<<m_currentIndex<<
") out of range ("<<m_elements.count()<<
")"<<endl;
184 }
else if (m_currentIndex > 0) {
185 AccountItem *i =
static_cast<AccountItem*
>(m_elements[accountsComboBox->text(m_currentIndex)]);
187 i->isDefault =
false;
190 if (index < (
int)m_elements.size()) {
191 AccountItem *i =
static_cast<AccountItem*
>(m_elements[accountsComboBox->currentText()]);
194 m_currentIndex = index;
201void AccountsPanel::slotChanged() {
205void AccountsPanel::slotSelectionChanged() {
208 removeBtn->setEnabled(
false);
209 newBtn->setEnabled(
false);
210 subBtn->setEnabled(
false);
211 accountList->setSelected(m_renameItem,
true);
214 if (accountList->childCount() == 0) {
215 removeBtn->setEnabled(
false);
216 newBtn->setEnabled(
true);
217 subBtn->setEnabled(
false);
220 TQListViewItem *i = accountList->selectedItem();
221 removeBtn->setEnabled((
bool)i);
222 newBtn->setEnabled(
true);
223 subBtn->setEnabled((
bool)i);
226void AccountsPanel::slotItemRenamed(TQListViewItem *item,
int col) {
228 item->setRenameEnabled(col,
false);
235 if (item->text(0).isEmpty()) {
236 item->setText(0,
static_cast<AccountItem*
>(item)->oldText);
238 if (item->text(0).isEmpty()) {
241 emit startRename(item, 0);
244 if (!isUnique(item)) {
246 emit startRename(item, 0);
250 removeBtn->setEnabled(accountList->selectedItem());
251 newBtn->setEnabled(accountList->selectedItem());
252 subBtn->setEnabled(accountList->selectedItem());
257bool AccountsPanel::isUnique(TQListViewItem *item) {
258 TQListViewItemIterator it(accountList);
259 for (; it.current(); ++it) {
260 if (it.current() != item && it.current()->text(0) == item->text(0)) {
267void AccountsPanel::slotRemoveBtn() {
268 slotRemoveItem(accountList->selectedItem());
272void AccountsPanel::slotNewBtn() {
274 TQListViewItem *item = accountList->selectedItem();
275 if (item && item->text(0).isEmpty()) {
280 if (item->parent()) {
281 n =
new AccountItem(*
this, item->parent(), item);
283 n =
new AccountItem(*
this, accountList, item);
286 n =
new AccountItem(*
this, accountList);
288 slotListDoubleClicked(n, TQPoint(), 0);
291void AccountsPanel::slotSubBtn() {
293 TQListViewItem *item = accountList->selectedItem();
294 if (item && item->text(0).isEmpty()) {
299 n =
new AccountItem(*
this, item);
301 n =
new AccountItem(*
this, accountList);
303 slotListDoubleClicked(n, TQPoint(), 0);
306KCommand *AccountsPanel::buildCommand(Part *part) {
307 KMacroCommand *cmd = 0;
309 TQPtrListIterator<TQListViewItem> rit = m_removedItems;
310 for (;rit.current(); ++rit) {
311 AccountItem *item =
static_cast<AccountItem*
>(rit.current());
313 if (!cmd) cmd =
new KMacroCommand(i18n(
"Modify Accounts"));
314 cmd->addCommand(
new RemoveAccountCmd(part, part->getProject(), item->account));
316 m_removedItems.setAutoDelete(
true);
318 KCommand *c = save(part, part->getProject());
320 if (!cmd) cmd =
new KMacroCommand(i18n(
"Modify Accounts"));
326KCommand *AccountsPanel::save(Part *part, Project &project) {
327 KMacroCommand *cmd=0;
328 TQListViewItem *myChild = accountList->firstChild();
329 for (; myChild; myChild = myChild->nextSibling()) {
330 KCommand *c = save(part, project, myChild);
332 if (!cmd) cmd =
new KMacroCommand(
"");
339KCommand *AccountsPanel::save(Part *part, Project &project, TQListViewItem *i) {
340 KMacroCommand *cmd=0;
341 AccountItem *item =
static_cast<AccountItem*
>(i);
342 if (item->account == 0) {
343 if (!item->text(0).isEmpty()) {
345 if (!cmd) cmd =
new KMacroCommand(
"");
346 item->account =
new Account(item->text(0), item->text(1));
347 if (item->parent()) {
349 cmd->addCommand(
new AddAccountCmd(part, project, item->account, item->parent()->text(0)));
351 cmd->addCommand(
new AddAccountCmd(part, project, item->account));
355 if (!item->text(0).isEmpty() && (item->text(0) != item->account->name())) {
356 if (!cmd) cmd =
new KMacroCommand(
"");
358 cmd->addCommand(
new RenameAccountCmd(part, item->account, item->text(0)));
360 if (item->text(1) != item->account->description()) {
361 if (!cmd) cmd =
new KMacroCommand(
"");
363 cmd->addCommand(
new ModifyAccountDescriptionCmd(part, item->account, item->text(1)));
366 TQListViewItem *myChild = item->firstChild();
367 for (; myChild; myChild = myChild->nextSibling()) {
368 KCommand *c = save(part, project, myChild);
370 if (!cmd) cmd =
new KMacroCommand(
"");
374 AccountItem *ai =
static_cast<AccountItem*
>(m_elements[accountsComboBox->currentText()]);
375 Account *newDefaultAccount = 0;
377 newDefaultAccount = ai->account;
379 if (m_oldDefaultAccount != newDefaultAccount) {
380 if (!cmd) cmd =
new KMacroCommand(
"");
381 cmd->addCommand(
new ModifyDefaultAccountCmd(part, m_accounts, m_oldDefaultAccount, newDefaultAccount));
386void AccountsPanel::slotListDoubleClicked(TQListViewItem* item,
const TQPoint&,
int col) {
390 slotStartRename(item, col);
393void AccountsPanel::slotRenameStarted(TQListViewItem *,
int ) {
395 if (accountList->isRenaming()) {
396 removeBtn->setEnabled(
false);
397 newBtn->setEnabled(
false);
398 subBtn->setEnabled(
false);
402void AccountsPanel::slotStartRename(TQListViewItem *item,
int col) {
404 static_cast<AccountItem*
>(item)->oldText = item->text(col);
405 item->setRenameEnabled(col,
true);
406 item->startRename(col);
409 emit renameStarted(item, col);
412void AccountsPanel::slotRemoveItem(TQListViewItem *i) {
413 AccountItem *item =
static_cast<AccountItem*
>(i);
418 TQListViewItem *p = item->parent();
422 m_removedItems.append(item);
426 if (p->childCount() == 0) {
430 accountList->takeItem(item);
432 m_removedItems.append(item);
440void AccountsPanel::renameStopped(TQListViewItem *) {
443 emit selectionChanged();
446void AccountsPanel::slotOk() {
452#include "kptaccountspanel.moc"
Accounts administrates all accounts.