C++ only uses NULL because C does. The C++ way is to use 0.
[shopper.git] / src / ui / LabelEntry.cc
blobe4106e493d45ef450964aa6d383f96ba742c2efb
1 /* Shopper
2 * Copyright (C) 2008 David Greaves <david@dgreaves.com>
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 "GestureWatcher.h"
24 #include <QWidget>
25 #include <QLabel>
26 #include <QString>
27 #include <QMouseEvent>
28 #include <QLineEdit>
29 #include <QSizePolicy>
30 #include <QFont>
32 namespace Shopper
34 // SLabel a label that emits pressed() when clicked
35 SLabel::SLabel(QWidget * parent, Qt::WindowFlags f ) :
36 QLabel(parent, f)
39 SLabel::SLabel( const QString & text, QWidget * parent, Qt::WindowFlags f ) :
40 QLabel(text, parent, f)
43 void SLabel::setVisible(bool vis)
45 QLabel::setVisible(vis);
47 void SLabel::mouseReleaseEvent ( QMouseEvent * event )
49 if (event->button() == Qt::LeftButton) {
50 event->accept();
51 emit pressed();
53 event->ignore();
56 // LabelEntry - a label that turns into a textbox when clicked
57 LabelEntry::LabelEntry(QWidget *parent) :
58 QWidget(parent),
59 data(""),
60 entry(0),
61 layout(this)
63 init();
65 LabelEntry::LabelEntry(const QString & value, QWidget *parent) :
66 QWidget(parent),
67 data(value),
68 entry(0),
69 layout(this)
71 init();
73 void LabelEntry::init()
75 myLabel = new SLabel(data, this);
76 GestureWatcher* gw = GestureWatcher::getGestureWatcher();
77 gw->connect(myLabel, Gesture("r l "),
78 this, SLOT(label_clicked()));
79 gw->connect(myLabel, Gesture("l r "),
80 this, SLOT(label_clicked()));
81 // connect(myLabel, SIGNAL(pressed()),
82 // this, SLOT(label_clicked()));
83 layout.addWidget(myLabel);
84 layout.setContentsMargins(0, 0, 0, 0);
85 myLabel->show();
87 QString LabelEntry::getText()
89 return data;
92 void LabelEntry::label_clicked()
94 _ENTER;
95 if (entry != 0) return; // events may arrive post-creation
96 entry = new QLineEdit(this);
97 entry->setSizePolicy(myLabel->sizePolicy());
98 // entry->set_activates_default(false);
99 // entry->set_width_chars(15); // FIXME
100 entry->setText(data);
101 // self-destruct on unfocus or activate
102 connect(entry, SIGNAL(editingFinished()),
103 this, SLOT(entry_finished()));
105 layout.removeWidget(myLabel);
106 myLabel->hide();
107 layout.addWidget(entry);
108 entry->show();
109 entry->setFocus(Qt::MouseFocusReason);
111 void LabelEntry::entry_finished()
113 if (entry == 0) return; // events may arrive post-deletion
114 data=entry->text();
115 myLabel->setText(data);
116 layout.removeWidget(entry);
117 entry->hide();
118 layout.addWidget(myLabel);
119 myLabel->show();
120 entry->deleteLater(); // We can now forget about it...
121 entry=0;
122 emit changed();
124 void LabelEntry::setAlignment ( Qt::Alignment al )
126 myLabel->setAlignment(al);
128 void LabelEntry::setText ( const QString & d )
130 data = d;
132 void LabelEntry::setVisible(bool vis)
134 QWidget::setVisible(vis);
137 void LabelEntry::setFrameStyle(int i)
139 myLabel->setFrameStyle(i);
141 void LabelEntry::setSizePolicy(QSizePolicy::Policy h, QSizePolicy::Policy v)
143 QWidget::setSizePolicy(h,v);
144 myLabel->setSizePolicy(h,v);
146 void LabelEntry::setSizePolicy(QSizePolicy p)
148 QWidget::setSizePolicy(p);
149 myLabel->setSizePolicy(p);
151 void LabelEntry::setFont ( const QFont & f)
153 myLabel->setFont(f);
154 if (entry) entry->setFont(f);
156 QLabel* LabelEntry::label () { return myLabel; }