00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kptaccountspanel.h"
00021 #include "kptaccount.h"
00022 #include "kptcommand.h"
00023 #include "kptproject.h"
00024
00025 #include <QComboBox>
00026 #include <QHeaderView>
00027 #include <QTreeWidget>
00028 #include <QPushButton>
00029 #include <QString>
00030 #include <QStringList>
00031
00032 #include <klocale.h>
00033
00034 #include <kdebug.h>
00035
00036 namespace KPlato
00037 {
00038
00039 class AccountItem : public QTreeWidgetItem {
00040 public:
00041 AccountItem(AccountsPanel &pan, QTreeWidget *parent)
00042 : QTreeWidgetItem(parent), account(0), panel(pan)
00043 { init(); }
00044 AccountItem(AccountsPanel &pan, QTreeWidgetItem *parent)
00045 : QTreeWidgetItem(parent), account(0), panel(pan)
00046 { init(); }
00047 AccountItem(AccountsPanel &pan, QTreeWidget *parent, QString label1, QString label2 = QString::null)
00048 : QTreeWidgetItem(parent), account(0), panel(pan) {
00049 setText(0, label1);
00050 setText(1, label2);
00051 init();
00052 }
00053 AccountItem(AccountsPanel &pan, QTreeWidgetItem *parent, QString label1, QString label2 = QString::null)
00054 : QTreeWidgetItem(parent), account(0), panel(pan) {
00055 setText(0, label1);
00056 setText(1, label2);
00057 init();
00058 }
00059 AccountItem(AccountsPanel &pan, QTreeWidget *parent, QTreeWidgetItem *after)
00060 : QTreeWidgetItem(parent, after), account(0), panel(pan)
00061 { init(); }
00062 AccountItem(AccountsPanel &pan, QTreeWidgetItem *parent, QTreeWidgetItem *after)
00063 : QTreeWidgetItem(parent, after), account(0), panel(pan)
00064 { init(); }
00065
00066 Account *account;
00067 bool isDefault;
00068 AccountsPanel &panel;
00069
00070 private:
00071 void init() {
00072 isDefault = false;
00073 setFlags(flags() | Qt::ItemIsEditable);
00074 }
00075 };
00076
00077 AccountsPanel::AccountsPanel(Accounts &acc, QWidget *p)
00078 : AccountsPanelBase(p),
00079 m_accounts(acc),
00080 m_currentIndex(0),
00081 m_renameItem(0)
00082 {
00083 accountList->setHeaderLabels(QStringList()<<"Account"<<"Description");
00084 accountList->header()->setStretchLastSection(true);
00085 addItems(accountList, acc);
00086
00087 slotSelectionChanged();
00088
00089 connect(accountList, SIGNAL(itemSelectionChanged()), SLOT(slotSelectionChanged()));
00090
00091 connect(removeBtn, SIGNAL(clicked()), SLOT(slotRemoveBtn()));
00092 connect(newBtn, SIGNAL(clicked()), SLOT(slotNewBtn()));
00093 connect(subBtn, SIGNAL(clicked()), SLOT(slotSubBtn()));
00094
00095 connect(accountsComboBox, SIGNAL(activated(int)), SLOT(slotActivated(int)));
00096
00097 connect(accountList, SIGNAL(itemChanged(QTreeWidgetItem*, int)), SLOT(slotItemChanged(QTreeWidgetItem*, int)));
00098 }
00099
00100 void AccountsPanel::addItems(QTreeWidget *lv, Accounts &acc) {
00101
00102 AccountListIterator it = acc.accountList();
00103 foreach (Account *a, acc.accountList()) {
00104 QString n = a->name();
00105 QString d = a->description();
00106 AccountItem *item = new AccountItem(*this, lv, n, d);
00107 item->account = a;
00108 item->isDefault = (a == acc.defaultAccount());
00109 if (a->isElement()) {
00110 addElement(item);
00111 }
00112 addItems(item, a);
00113 }
00114 }
00115
00116 void AccountsPanel::addItems(QTreeWidgetItem *item, Account *acc) {
00117 foreach (Account *a, acc->accountList()) {
00118 QString n = a->name();
00119 QString d = a->description();
00120 AccountItem *ai = new AccountItem(*this, item, n, d);
00121 ai->account = a;
00122 ai->isDefault = (a == acc->list()->defaultAccount());
00123 if (a->isElement()) {
00124 addElement(ai);
00125 }
00126 addItems(ai, a);
00127 }
00128 }
00129
00130 void AccountsPanel::addElement(QTreeWidgetItem *item) {
00131 if (item->parent()) {
00132 removeElement(item->parent());
00133 }
00134 m_elements.insert(item->text(0), item);
00135
00136 refreshDefaultAccount();
00137 }
00138
00139 void AccountsPanel::removeElement(QString key) {
00140 QHash<QString, QTreeWidgetItem*>::Iterator it = m_elements.find(key);
00141 for (it = m_elements.begin(); it != m_elements.end(); ++it) {
00142 removeElement(*it);
00143 }
00144 }
00145
00146 void AccountsPanel::removeElement(QTreeWidgetItem *item) {
00147 static_cast<AccountItem*>(item)->isDefault = false;
00148 m_elements.remove(item->text(0));
00149 refreshDefaultAccount();
00150 }
00151
00152 void AccountsPanel::refreshDefaultAccount() {
00153 accountsComboBox->clear();
00154 m_currentIndex = 0;
00155 accountsComboBox->addItem(i18n("None"));
00156 QStringList keylist = m_elements.uniqueKeys();
00157 int i=1;
00158 foreach (QString key, keylist) {
00159 accountsComboBox->addItem(key);
00160 if (static_cast<AccountItem*>(m_elements[key])->isDefault) {
00161 m_currentIndex = i;
00162 accountsComboBox->setCurrentIndex(i);
00163
00164 }
00165 ++i;
00166 }
00167
00168 }
00169
00170 void AccountsPanel::slotItemChanged(QTreeWidgetItem* item, int col) {
00171
00172 emit changed(true);
00173 }
00174
00175 void AccountsPanel::slotActivated(int index) {
00176
00177 if (m_currentIndex >= (int)m_elements.count()) {
00178 kError()<<k_funcinfo<<"currentIndex ("<<m_currentIndex<<") out of range ("<<m_elements.count()<<")"<<endl;
00179 } else if (m_currentIndex > 0) {
00180 AccountItem *i = static_cast<AccountItem*>(m_elements[accountsComboBox->text(m_currentIndex)]);
00181 if (i)
00182 i->isDefault = false;
00183 }
00184 m_currentIndex = 0;
00185 if (index < (int)m_elements.size()) {
00186 AccountItem *i = static_cast<AccountItem*>(m_elements[accountsComboBox->currentText()]);
00187 if (i) {
00188 i->isDefault = true;
00189 m_currentIndex = index;
00190
00191 }
00192 }
00193 slotChanged();
00194 }
00195
00196 void AccountsPanel::slotChanged() {
00197 emit changed(true);
00198 }
00199
00200 void AccountsPanel::slotSelectionChanged() {
00201
00202 if (accountList->topLevelItemCount() == 0) {
00203 removeBtn->setEnabled(false);
00204 newBtn->setEnabled(true);
00205 subBtn->setEnabled(false);
00206 return;
00207 }
00208 bool i = accountList->selectedItems().count() > 0;
00209 removeBtn->setEnabled((bool)i);
00210 newBtn->setEnabled(true);
00211 subBtn->setEnabled((bool)i);
00212 }
00213
00214 bool AccountsPanel::isUnique(QTreeWidgetItem *item) {
00215
00216
00217
00218
00219
00220
00221
00222 return false;
00223 }
00224
00225 void AccountsPanel::slotRemoveBtn() {
00226 QList<QTreeWidgetItem*> lst = accountList->selectedItems();
00227 foreach (QTreeWidgetItem* i, lst) {
00228 slotRemoveItem(i);
00229 }
00230 slotChanged();
00231 }
00232
00233 void AccountsPanel::slotNewBtn() {
00234
00235 QList<QTreeWidgetItem*> lst = accountList->selectedItems();
00236 QTreeWidgetItem *item = 0;
00237 if (lst.count() > 0) {
00238 item = lst[0];
00239 }
00240 QTreeWidgetItem *n;
00241 if (item) {
00242 if (item->parent()) {
00243 n = new AccountItem(*this, item->parent(), item);
00244 } else {
00245 n = new AccountItem(*this, accountList, item);
00246 }
00247 } else {
00248 n = new AccountItem(*this, accountList);
00249 }
00250 accountList->clearSelection();
00251 n->setSelected(true);
00252 accountList->editItem(n);
00253 }
00254
00255 void AccountsPanel::slotSubBtn() {
00256
00257 QList<QTreeWidgetItem*> lst = accountList->selectedItems();
00258 QTreeWidgetItem* item = 0;
00259 if (lst.count() > 0) {
00260 item = lst[0];
00261 }
00262 QTreeWidgetItem *n;
00263 if (item) {
00264 n = new AccountItem(*this, item);
00265 item->setExpanded(true);
00266 } else {
00267 n = new AccountItem(*this, accountList);
00268 }
00269 accountList->clearSelection();
00270 n->setSelected(true);
00271 accountList->editItem(n);
00272 }
00273
00274 KCommand *AccountsPanel::buildCommand(Part *part) {
00275 KMacroCommand *cmd = 0;
00276
00277 while (!m_removedItems.isEmpty()) {
00278 AccountItem *item = static_cast<AccountItem*>(m_removedItems.takeFirst());
00279
00280 if (!cmd) cmd = new KMacroCommand(i18n("Modify Accounts"));
00281 cmd->addCommand(new RemoveAccountCmd(part, part->getProject(), item->account));
00282 delete item;
00283 }
00284
00285 KCommand *c = save(part, part->getProject());
00286 if (c) {
00287 if (!cmd) cmd = new KMacroCommand(i18n("Modify Accounts"));
00288 cmd->addCommand(c);
00289 }
00290 return cmd;
00291 }
00292
00293 KCommand *AccountsPanel::save(Part *part, Project &project) {
00294 KMacroCommand *cmd=0;
00295 int cnt = accountList->topLevelItemCount();
00296 for (int i=0; i < cnt; ++i) {
00297 KCommand *c = save(part, project, accountList->topLevelItem(i));
00298 if (c) {
00299 if (!cmd) cmd = new KMacroCommand("");
00300 cmd->addCommand(c);
00301 }
00302 }
00303 return cmd;
00304 }
00305
00306 KCommand *AccountsPanel::save(Part *part, Project &project, QTreeWidgetItem *i) {
00307 KMacroCommand *cmd=0;
00308 AccountItem *item = static_cast<AccountItem*>(i);
00309 if (item->account == 0) {
00310 if (!item->text(0).isEmpty()) {
00311
00312 if (!cmd) cmd = new KMacroCommand("");
00313 item->account = new Account(item->text(0), item->text(1));
00314 if (item->parent()) {
00315
00316 cmd->addCommand(new AddAccountCmd(part, project, item->account, item->parent()->text(0)));
00317 } else {
00318 cmd->addCommand(new AddAccountCmd(part, project, item->account));
00319 }
00320 }
00321 } else {
00322 if (!item->text(0).isEmpty() && (item->text(0) != item->account->name())) {
00323 if (!cmd) cmd = new KMacroCommand("");
00324
00325 cmd->addCommand(new RenameAccountCmd(part, item->account, item->text(0)));
00326 }
00327 if (item->text(1) != item->account->description()) {
00328 if (!cmd) cmd = new KMacroCommand("");
00329
00330 cmd->addCommand(new ModifyAccountDescriptionCmd(part, item->account, item->text(1)));
00331 }
00332 }
00333 int cnt = item->childCount();
00334 for (int i=0; i < cnt; ++i) {
00335 QTreeWidgetItem *myChild = item->child(i);
00336 KCommand *c = save(part, project, myChild);
00337 if (c) {
00338 if (!cmd) cmd = new KMacroCommand("");
00339 cmd->addCommand(c);
00340 }
00341 }
00342 AccountItem *ai = static_cast<AccountItem*>(m_elements[accountsComboBox->currentText()]);
00343 Account *newDefaultAccount = 0;
00344 if (ai) {
00345 newDefaultAccount = ai->account;
00346 }
00347 if (m_oldDefaultAccount != newDefaultAccount) {
00348 if (!cmd) cmd = new KMacroCommand("");
00349 cmd->addCommand(new ModifyDefaultAccountCmd(part, m_accounts, m_oldDefaultAccount, newDefaultAccount));
00350 }
00351 return cmd;
00352 }
00353
00354 void AccountsPanel::slotRemoveItem(QTreeWidgetItem *i) {
00355 AccountItem *item = static_cast<AccountItem*>(i);
00356 if (item == 0)
00357 return;
00358
00359 removeElement(item);
00360 QTreeWidgetItem *p = item->parent();
00361 if (p) {
00362 p->takeChild(p->indexOfChild(item));
00363 if (item->account) {
00364 m_removedItems.append(item);
00365 } else {
00366 delete item;
00367 }
00368 if (p->childCount() == 0) {
00369 addElement(p);
00370 }
00371 } else {
00372 accountList->takeTopLevelItem(accountList->indexOfTopLevelItem(item));
00373 if (item->account) {
00374 m_removedItems.append(item);
00375 } else {
00376 delete item;
00377 }
00378 }
00379 }
00380
00381 void AccountsPanel::slotOk() {
00382
00383 }
00384
00385 }
00386
00387 #include "kptaccountspanel.moc"