The class declaration should be the first header in the cc file for
[shopper.git] / src / ui / LabelEntry.cc
blob770c5dcf974118da8f41bf8bbe3ceb564e2ea8c9
1 /* Shopper
2 * Copyright (C) 2008 David Greaves
4 * This software is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2.1 of
7 * the License, or (at your option) any later version.
9 * This software is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this software; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17 * 02110-1301 USA
20 //#define DEBUG_SHOPPER 1
21 #include "LabelEntry.h"
22 #include "shopper.h" // automake, i8n, gettext
23 #include <QtGui>
24 #include "GestureWatcher.h"
26 namespace Shopper
28 // SLabel a label that emits pressed() when clicked
29 SLabel::SLabel(QWidget * parent, Qt::WindowFlags f ) :
30 QLabel(parent, f)
33 SLabel::SLabel( const QString & text, QWidget * parent, Qt::WindowFlags f ) :
34 QLabel(text, parent, f)
37 void SLabel::setVisible(bool vis)
39 QLabel::setVisible(vis);
41 void SLabel::mouseReleaseEvent ( QMouseEvent * event )
43 if (event->button() == Qt::LeftButton) {
44 event->accept();
45 emit pressed();
47 event->ignore();
50 // LabelEntry - a label that turns into a textbox when clicked
51 LabelEntry::LabelEntry(QWidget *parent) :
52 QWidget(parent),
53 data(""),
54 entry(NULL),
55 layout(this)
57 init();
59 LabelEntry::LabelEntry(const QString & value, QWidget *parent) :
60 QWidget(parent),
61 data(value),
62 entry(NULL),
63 layout(this)
65 init();
67 void LabelEntry::init()
69 myLabel = new SLabel(data, this);
70 GestureWatcher* gw = GestureWatcher::getGestureWatcher();
71 gw->connect(myLabel, Gesture("r l "),
72 this, SLOT(label_clicked()));
73 gw->connect(myLabel, Gesture("l r "),
74 this, SLOT(label_clicked()));
75 // connect(myLabel, SIGNAL(pressed()),
76 // this, SLOT(label_clicked()));
77 layout.addWidget(myLabel);
78 layout.setContentsMargins(0, 0, 0, 0);
79 myLabel->show();
81 QString LabelEntry::getText()
83 return data;
86 void LabelEntry::label_clicked()
88 _ENTER;
89 if (entry != NULL) return; // events may arrive post-creation
90 entry = new QLineEdit(this);
91 entry->setSizePolicy(myLabel->sizePolicy());
92 // entry->set_activates_default(false);
93 // entry->set_width_chars(15); // FIXME
94 entry->setText(data);
95 // self-destruct on unfocus or activate
96 connect(entry, SIGNAL(editingFinished()),
97 this, SLOT(entry_finished()));
99 layout.removeWidget(myLabel);
100 myLabel->hide();
101 layout.addWidget(entry);
102 entry->show();
103 entry->setFocus(Qt::MouseFocusReason);
105 void LabelEntry::entry_finished()
107 if (entry == NULL) return; // events may arrive post-deletion
108 data=entry->text();
109 myLabel->setText(data);
110 layout.removeWidget(entry);
111 entry->hide();
112 layout.addWidget(myLabel);
113 myLabel->show();
114 entry->deleteLater(); // We can now forget about it...
115 entry=NULL;
116 emit changed();
118 void LabelEntry::setAlignment ( Qt::Alignment al )
120 myLabel->setAlignment(al);
122 void LabelEntry::setText ( const QString & d )
124 data = d;
126 void LabelEntry::setVisible(bool vis)
128 QWidget::setVisible(vis);
131 void LabelEntry::setFrameStyle(int i)
133 myLabel->setFrameStyle(i);
135 void LabelEntry::setSizePolicy(QSizePolicy::Policy h, QSizePolicy::Policy v)
137 QWidget::setSizePolicy(h,v);
138 myLabel->setSizePolicy(h,v);
140 void LabelEntry::setSizePolicy(QSizePolicy p)
142 QWidget::setSizePolicy(p);
143 myLabel->setSizePolicy(p);
145 void LabelEntry::setFont ( const QFont & f)
147 myLabel->setFont(f);
148 if (entry) entry->setFont(f);
150 QLabel* LabelEntry::label () { return myLabel; }