summaryrefslogtreecommitdiffstats
path: root/src/gui/linefieldwidget.cpp
blob: 86b0ab8d9696281eb08e3f31b02f472de5d327eb (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
/***************************************************************************
    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 "linefieldwidget.h"
#include "../field.h"
#include "../isbnvalidator.h"
#include "../fieldcompletion.h"
#include "../latin1literal.h"
#include "../tellico_kernel.h"
#include "../gui/lineedit.h"

using Tellico::GUI::LineFieldWidget;

LineFieldWidget::LineFieldWidget(Data::FieldPtr field_, TQWidget* parent_, const char* name_/*=0*/)
    : FieldWidget(field_, parent_, name_) {

  m_lineEdit = new GUI::LineEdit(this);
  m_lineEdit->setAllowSpellCheck(true);
  m_lineEdit->setEnableSpellCheck(field_->formatFlag() != Data::Field::FormatName);
  connect(m_lineEdit, TQ_SIGNAL(textChanged(const TQString&)), TQ_SIGNAL(modified()));

  registerWidget();

  if(field_->flags() & Data::Field::AllowCompletion) {
    FieldCompletion* completion = new FieldCompletion(field_->flags() & Data::Field::AllowMultiple);
    completion->setItems(Kernel::self()->valuesByFieldName(field_->name()));
    completion->setIgnoreCase(true);
    m_lineEdit->setCompletionObject(completion);
    m_lineEdit->setAutoDeleteCompletionObject(true);
  }

  if(field_->name() == Latin1Literal("isbn")) {
    m_lineEdit->setValidator(new ISBNValidator(this));
  }
}

TQString LineFieldWidget::text() const {
  TQString text = m_lineEdit->text();
  if(field()->flags() & Data::Field::AllowMultiple) {
    text.replace(s_semiColon, TQString::fromLatin1("; "));
  }
  return text.stripWhiteSpace();
}

void LineFieldWidget::setText(const TQString& text_) {
  blockSignals(true);
  m_lineEdit->blockSignals(true);
  m_lineEdit->setText(text_);
  m_lineEdit->blockSignals(false);
  blockSignals(false);
}

void LineFieldWidget::clear() {
  m_lineEdit->clear();
  editMultiple(false);
}

void LineFieldWidget::addCompletionObjectItem(const TQString& text_) {
  m_lineEdit->completionObject()->addItem(text_);
}

void LineFieldWidget::updateFieldHook(Data::FieldPtr oldField_, Data::FieldPtr newField_) {
  bool wasComplete = (oldField_->flags() & Data::Field::AllowCompletion);
  bool isComplete = (newField_->flags() & Data::Field::AllowCompletion);
  if(!wasComplete && isComplete) {
    FieldCompletion* completion = new FieldCompletion(isComplete);
    completion->setItems(Kernel::self()->valuesByFieldName(newField_->name()));
    completion->setIgnoreCase(true);
    m_lineEdit->setCompletionObject(completion);
    m_lineEdit->setAutoDeleteCompletionObject(true);
  } else if(wasComplete && !isComplete) {
    m_lineEdit->completionObject()->clear();
  }
}

TQWidget* LineFieldWidget::widget() {
  return m_lineEdit;
}

#include "linefieldwidget.moc"