summaryrefslogtreecommitdiffstats
path: root/src/borrowerdialog.cpp
blob: 4346b6b710ade11138f3c56fdaff6543b8412e8f (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                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 <klocale.h>
#include <klineedit.h>
#include <kabc/addressee.h>
#include <kabc/stdaddressbook.h>
#include <kiconloader.h>

#include <qlayout.h>

using Tellico::BorrowerDialog;

BorrowerDialog::Item::Item(KListView* parent_, const KABC::Addressee& add_)
    : KListViewItem(parent_), m_uid(add_.uid()) {
  setText(0, add_.realName());
  setPixmap(0, SmallIcon(QString::fromLatin1("kaddressbook")));
}

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

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

  m_listView = new KListView(mainWidget);
  topLayout->addWidget(m_listView);
  m_listView->addColumn(i18n("Name"));
  m_listView->setFullWidth(true);
  connect(m_listView, SIGNAL(doubleClicked(QListViewItem*)), SLOT(slotOk()));
  connect(m_listView, SIGNAL(selectionChanged(QListViewItem*)), SLOT(updateEdit(QListViewItem*)));

  m_lineEdit = new KLineEdit(mainWidget);
  topLayout->addWidget(m_lineEdit);
  connect(m_lineEdit->completionObject(), SIGNAL(match(const QString&)),
          SLOT(selectItem(const QString&)));
  m_lineEdit->setFocus();
  m_lineEdit->completionObject()->setIgnoreCase(true);

  KABC::AddressBook* abook = KABC::StdAddressBook::self(true);
  connect(abook, SIGNAL(addressBookChanged(AddressBook*)),
          SLOT(slotLoadAddressBook()));
  connect(abook, SIGNAL(loadingFinished(Resource*)),
          SLOT(slotLoadAddressBook()));
  slotLoadAddressBook();

  setMinimumWidth(400);
}

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

  const KABC::AddressBook* const abook = KABC::StdAddressBook::self(true);
  for(KABC::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 QString& str_) {
  if(str_.isEmpty()) {
    return;
  }

  QListViewItem* 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(QListViewItem* 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(QWidget* parent_) {
  BorrowerDialog dlg(parent_);

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

#include "borrowerdialog.moc"