Added a Notify class
[shopper.git] / src / ui / LabelEntry.cc
blob314675f7027742912f99f2038ba0cc7e230b99f4
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 "shopper.h" // automake, i8n, gettext
22 #include "LabelEntry.h"
24 namespace Shopper
26 // SLabel a label that emits pressed() when clicked
27 SLabel::SLabel(QWidget * parent, Qt::WindowFlags f ) :
28 QLabel(parent, f)
31 SLabel::SLabel( const QString & text, QWidget * parent, Qt::WindowFlags f ) :
32 QLabel(text, parent, f)
35 void SLabel::setVisible(bool vis)
37 _ENTER;
38 QLabel::setVisible(vis);
40 void SLabel::mousePressEvent ( QMouseEvent * event )
42 if (event->button() == Qt::LeftButton) emit pressed();
45 // LabelEntry - a label that turns into a textbox when clicked
46 LabelEntry::LabelEntry(QWidget *parent) :
47 QWidget(parent),
48 data(""),
49 entry(NULL),
50 layout(this)
52 init();
54 LabelEntry::LabelEntry(const QString & value, QWidget *parent) :
55 QWidget(parent),
56 data(value),
57 entry(NULL),
58 layout(this)
60 init();
62 void LabelEntry::init()
64 label = new SLabel(data, this);
65 connect(label, SIGNAL(pressed()),
66 this, SLOT(label_clicked()));
67 layout.addWidget(label);
68 layout.setContentsMargins(0, 0, 0, 0);
69 label->show();
71 QString LabelEntry::getText()
73 return data;
76 void LabelEntry::label_clicked()
78 _ENTER;
79 if (entry != NULL) return; // events may arrive post-creation
80 entry = new QLineEdit(this);
81 entry->setSizePolicy(label->sizePolicy());
82 // entry->set_activates_default(false);
83 // entry->set_width_chars(15); // FIXME
84 entry->setText(data);
85 // self-destruct on unfocus or activate
86 connect(entry, SIGNAL(editingFinished()),
87 this, SLOT(entry_finished()));
89 layout.removeWidget(label);
90 label->hide();
91 layout.addWidget(entry);
92 entry->show();
93 entry->setFocus(Qt::MouseFocusReason);
95 void LabelEntry::entry_finished()
97 _ENTER;
98 if (entry == NULL) return; // events may arrive post-deletion
99 data=entry->text();
100 label->setText(data);
101 layout.removeWidget(entry);
102 entry->hide();
103 layout.addWidget(label);
104 label->show();
105 entry->deleteLater(); // We can now forget about it...
106 entry=NULL;
107 emit changed();
109 void LabelEntry::setAlignment ( Qt::Alignment al )
111 label->setAlignment(al);
113 void LabelEntry::setText ( const QString & d )
115 data = d;
117 void LabelEntry::setVisible(bool vis)
119 _ENTER;
120 QWidget::setVisible(vis);
123 void LabelEntry::setFrameStyle(int i)
125 label->setFrameStyle(i);
127 void LabelEntry::setSizePolicy(QSizePolicy::Policy h, QSizePolicy::Policy v)
129 QWidget::setSizePolicy(h,v);
130 label->setSizePolicy(h,v);
132 void LabelEntry::setSizePolicy(QSizePolicy p)
134 _ENTER;
135 QWidget::setSizePolicy(p);
136 label->setSizePolicy(p);