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
20 //#define DEBUG_SHOPPER 1
21 #include "LabelEntry.h"
22 #include "shopper.h" // automake, i8n, gettext
23 #include "GestureWatcher.h"
27 #include <QMouseEvent>
29 #include <QSizePolicy>
34 // SLabel a label that emits pressed() when clicked
35 SLabel::SLabel(QWidget
* parent
, Qt::WindowFlags 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
) {
56 // LabelEntry - a label that turns into a textbox when clicked
57 LabelEntry::LabelEntry(QWidget
*parent
) :
65 LabelEntry::LabelEntry(const QString
& value
, QWidget
*parent
) :
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);
87 QString
LabelEntry::getText()
92 void LabelEntry::label_clicked()
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
);
107 layout
.addWidget(entry
);
109 entry
->setFocus(Qt::MouseFocusReason
);
111 void LabelEntry::entry_finished()
113 if (entry
== 0) return; // events may arrive post-deletion
115 myLabel
->setText(data
);
116 layout
.removeWidget(entry
);
118 layout
.addWidget(myLabel
);
120 entry
->deleteLater(); // We can now forget about it...
124 void LabelEntry::setAlignment ( Qt::Alignment al
)
126 myLabel
->setAlignment(al
);
128 void LabelEntry::setText ( const QString
& 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
)
154 if (entry
) entry
->setFont(f
);
156 QLabel
* LabelEntry::label () { return myLabel
; }