summaryrefslogtreecommitdiffstats
path: root/src/borrowerdialog.cpp
blob: b03b1dd4431cbd4f4b41d5f3cd026b153862744b (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
/***************************************************************************
    copyright            : (C) 2005-2006 by Robby Stephenson
    email                : robby@periapsis.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License as  *
 *   published by the Free Software Foundation;                            *
 *                                                                         *
 ***************************************************************************/

#include "borrowerdialog.h"
#include "document.h"
#include "collection.h"

#include <tdelocale.h>
#include <klineedit.h>
#include <tdeabc/addressee.h>
#include <tdeabc/stdaddressbook.h>
#include <kiconloader.h>

#include <tqlayout.h>

using Tellico::BorrowerDialog;

BorrowerDialog::Item::Item(TDEListView* parent_, const TDEABC::Addressee& add_)
    : TDEListViewItem(parent_), m_uid(add_.uid()) {
  setText(0, add_.realName());
  setPixmap(0, SmallIcon(TQString::fromLatin1("kaddressbook")));
}

BorrowerDialog::Item::Item(TDEListView* parent_, const Data::Borrower& bor_)
    : TDEListViewItem(parent_), m_uid(bor_.uid()) {
  setText(0, bor_.name());
  setPixmap(0, SmallIcon(TQString::fromLatin1("tellico")));
}

// default button is going to be used as a print button, so it's separated
BorrowerDialog::BorrowerDialog(TQWidget* parent_, const char* name_/*=0*/)
    : KDialogBase(parent_, name_, true, i18n("Select Borrower"), Ok|Cancel) {
  TQWidget* mainWidget = new TQWidget(this, "BorrowerDialog mainWidget");
  setMainWidget(mainWidget);
  TQVBoxLayout* topLayout = new TQVBoxLayout(mainWidget, 0, KDialog::spacingHint());

  m_listView = new TDEListView(mainWidget);
  topLayout->addWidget(m_listView);
  m_listView->addColumn(i18n("Name"));
  m_listView->setFullWidth(true);
  connect(m_listView, TQ_SIGNAL(doubleClicked(TQListViewItem*)), TQ_SLOT(slotOk()));
  connect(m_listView, TQ_SIGNAL(selectionChanged(TQListViewItem*)), TQ_SLOT(updateEdit(TQListViewItem*)));

  m_lineEdit = new KLineEdit(mainWidget);
  topLayout->addWidget(m_lineEdit);
  connect(m_lineEdit->completionObject(), TQ_SIGNAL(match(const TQString&)),
          TQ_SLOT(selectItem(const TQString&)));
  m_lineEdit->setFocus();
  m_lineEdit->completionObject()->setIgnoreCase(true);

  TDEABC::AddressBook* abook = TDEABC::StdAddressBook::self(true);
  connect(abook, TQ_SIGNAL(addressBookChanged(AddressBook*)),
          TQ_SLOT(slotLoadAddressBook()));
  connect(abook, TQ_SIGNAL(loadingFinished(Resource*)),
          TQ_SLOT(slotLoadAddressBook()));
  slotLoadAddressBook();

  setMinimumWidth(400);
}

void BorrowerDialog::slotLoadAddressBook() {
  m_listView->clear();
  m_itemDict.clear();
  m_lineEdit->completionObject()->clear();

  const TDEABC::AddressBook* const abook = TDEABC::StdAddressBook::self(true);
  for(TDEABC::AddressBook::ConstIterator it = abook->begin(), end = abook->end();
      it != end; ++it) {
    // skip people with no name
    if((*it).realName().isEmpty()) {
      continue;
    }
    Item* item = new Item(m_listView, *it);
    m_itemDict.insert((*it).realName(), item);
    m_lineEdit->completionObject()->addItem((*it).realName());
  }

  // add current borrowers, too
  const Data::BorrowerVec& borrowers = Data::Document::self()->collection()->borrowers();
  for(Data::BorrowerVec::ConstIterator it = borrowers.constBegin(); it != borrowers.constEnd(); ++it) {
    if(m_itemDict[it->name()]) {
      continue; // if an item already exists with this name
    }
    Item* item = new Item(m_listView, *it);
    m_itemDict.insert(it->name(), item);
    m_lineEdit->completionObject()->addItem(it->name());
  }
  m_listView->setSorting(0, true);
  m_listView->sort();
}

void BorrowerDialog::selectItem(const TQString& str_) {
  if(str_.isEmpty()) {
    return;
  }

  TQListViewItem* item = m_itemDict.find(str_);
  if(item) {
    m_listView->blockSignals(true);
    m_listView->setSelected(item, true);
    m_listView->ensureItemVisible(item);
    m_listView->blockSignals(false);
  }
}

void BorrowerDialog::updateEdit(TQListViewItem* item_) {
  m_lineEdit->setText(item_->text(0));
  m_lineEdit->setSelection(0, item_->text(0).length());
  m_uid = static_cast<Item*>(item_)->uid();
}

Tellico::Data::BorrowerPtr BorrowerDialog::borrower() {
  return new Data::Borrower(m_lineEdit->text(), m_uid);
}

// static
Tellico::Data::BorrowerPtr BorrowerDialog::getBorrower(TQWidget* parent_) {
  BorrowerDialog dlg(parent_);

  if(dlg.exec() == TQDialog::Accepted) {
    return dlg.borrower();
  }
  return 0;
}

#include "borrowerdialog.moc"