File: lineedit.cpp

package info (click to toggle)
nixnote2 2.0~beta11-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 9,448 kB
  • ctags: 7,058
  • sloc: cpp: 68,338; java: 1,096; sh: 834; makefile: 27
file content (193 lines) | stat: -rw-r--r-- 6,385 bytes parent folder | download
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/****************************************************************************
**
** Copyright (c) 2007 Trolltech ASA <info@trolltech.com>
**
** Use, modification and distribution is allowed without limitation,
** warranty, liability or support of any kind.
**
****************************************************************************/

#include "lineedit.h"
#include <QToolButton>
#include <QStyle>
#include <QPainter>
#include "global.h"

extern Global global;


 LineEdit::LineEdit(QWidget *parent)
     : QLineEdit(parent)
 {

     filterPosition = -1;
     clearButton = new QToolButton(this);
     QPixmap pixmap(global.getPixmapResource(":fileclose.png"));
     clearButton->setIcon(QIcon(pixmap));
     clearButton->setIconSize(pixmap.size());
     clearButton->setCursor(Qt::ArrowCursor);
     clearButton->setStyleSheet("QToolButton { border: none; padding: 0px; }");
     clearButton->hide();
     connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
     connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(updateCloseButton(const QString&)));
     int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
     setStyleSheet(QString("QLineEdit { padding-right: %1px; } ").arg(clearButton->sizeHint().width() + frameWidth + 1));
     QSize msz = minimumSizeHint();
     setMinimumSize(qMax(msz.width(), clearButton->sizeHint().height() + frameWidth * 2 + 2),
                    qMax(msz.height(), clearButton->sizeHint().height() + frameWidth * 2 + 2));

     defaultText = QString(tr("Search"));
     this->setText(defaultText);

     inactiveColor = "QLineEdit {color: gray; font:italic;} ";
     activeColor = "QLineEdit {color: black; font:normal;} ";

     connect(this, SIGNAL(returnPressed()), this, SLOT(buildSelection()));
     connect(this, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
     connect(clearButton, SIGNAL(clicked()), this, SLOT(buildSelection()));
     setStyleSheet(inactiveColor);
 }

 void LineEdit::resizeEvent(QResizeEvent *)
 {
     QSize sz = clearButton->sizeHint();
     int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
     clearButton->move(rect().right() - frameWidth - sz.width(),
                       (rect().bottom() + 1 - sz.height())/2);
 }

 void LineEdit::updateCloseButton(const QString& text)
 {
     clearButton->setVisible(!text.isEmpty());
 }




 //*************************************************************
 // This function is called when a user selects something
 // within this tree.  It also emits a signal that will
 // be picked up by the main nixnote program.
 //*************************************************************
 void LineEdit::buildSelection() {
     QLOG_TRACE() << "Inside LineEdit::buildSelection()";
     savedText = text().trimmed();

     // First, find out if we're already viewing history.  If we are we
     // chop off the end of the history & start a new one
     if (global.filterPosition+1 < global.filterCriteria.size()) {
         while (global.filterPosition+1 < global.filterCriteria.size())
             global.filterCriteria.removeLast();
     }

     filterPosition++;
     FilterCriteria *newFilter = new FilterCriteria();
     global.filterCriteria.push_back(newFilter);
     FilterCriteria *oldFilter = global.filterCriteria[global.filterPosition];
     global.filterPosition++;

     newFilter->setSearchString(text());
     if (!global.getClearNotebookOnSearch() && oldFilter->isNotebookSet())
         newFilter->setNotebook(*oldFilter->getNotebook());
     if (!global.getClearTagsOnSearch() && oldFilter->isTagsSet()) {
         QList<QTreeWidgetItem*> items = oldFilter->getTags();
         newFilter->setTags(items);
     }
     newFilter->resetTags=true;
     newFilter->resetNotebook=true;
     newFilter->resetAttribute = true;
     newFilter->resetFavorite = true;
     newFilter->resetDeletedOnly = true;
     newFilter->resetSavedSearch = true;
     QList<qint32> oldLids;
     oldFilter->getSelectedNotes(oldLids);
     newFilter->setSelectedNotes(oldLids);
     newFilter->setLid(oldFilter->getLid());

     emit updateSelectionRequested();

     QLOG_TRACE() << "Leaving LineEdit::buildSelection()";
 }


 //*************************************************************
 // This function is called from the main NixNote class.
 // it will reset the items which are selected based upon
 // what the user did somewhere else (outside this widget).
 //*************************************************************
 void LineEdit::updateSelection() {
     blockSignals(true);

     FilterCriteria *criteria = global.filterCriteria[global.filterPosition];
//     if (criteria->resetSearchString) {
//         this->blockSignals(true);
//         this->setText(defaultText);
//         setStyleSheet(inactiveColor);
//         this->blockSignals(false);
//     }
     if (global.filterPosition != filterPosition) {
         if (criteria->resetSearchString) {
             setText(defaultText);
             setStyleSheet(inactiveColor);
         }

         if (criteria->isSearchStringSet()) {
             setText(criteria->getSearchString());
             setStyleSheet(activeColor);
         }
     }
     filterPosition = global.filterPosition;

     blockSignals(false);
 }

void LineEdit::textChanged(QString text) {
    this->blockSignals(true);
    if (text.trimmed() == "" && !hasFocus())
        this->setText(defaultText);
    this->blockSignals(false);
    if ((text == defaultText || text == "") && savedText != "") {
        buildSelection();
    }
}

void LineEdit::focusInEvent(QFocusEvent *e)
{
  QLineEdit::focusInEvent(e);
  if (this->text() == defaultText) {
    blockSignals(true);
    setText("");
    blockSignals(false);
  }
  setStyleSheet(activeColor);
//  this->setCursor(Qt::ArrowCursor);
}

void LineEdit::focusOutEvent(QFocusEvent *e)
{
  QLineEdit::focusOutEvent(e);
  if (this->text().trimmed() == "") {
    blockSignals(true);
    setText(defaultText);
    blockSignals(false);
    setStyleSheet(inactiveColor);
  }
//  this->setCursor(Qt::PointingHandCursor);

}


// Check if any value is set
bool LineEdit::isSet() {
    if (this->text().trimmed() != defaultText && this->text().trimmed() != "")
        return true;
    else
        return false;
}




void LineEdit::reloadIcons() {
    clearButton->setIcon(global.getIconResource(":filecloseIcon"));
}