Moved QString debug to shopper.h with other DEBUG code
[shopper.git] / src / ui / LabelEntry.cc
blobc33200e6e3ed8a169a3907b531533ce9fadd89ec
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"
23 #include <QtGui>
25 #define SCROLL_SENSITIVITY 20
26 namespace Shopper
28 // SLabel a label that emits pressed() when clicked
29 SLabel::SLabel(QWidget * parent, Qt::WindowFlags f ) :
30 QLabel(parent, f),
31 start_event_x(0),
32 start_event_y(0),
33 scrolling(false)
36 SLabel::SLabel( const QString & text, QWidget * parent, Qt::WindowFlags f ) :
37 QLabel(text, parent, f),
38 start_event_x(0),
39 start_event_y(0),
40 scrolling(false)
43 void SLabel::setVisible(bool vis)
45 _ENTER;
46 QLabel::setVisible(vis);
48 void SLabel::mouseReleaseEvent ( QMouseEvent * event )
50 if (event->button() == Qt::LeftButton) {
51 event->accept();
52 if (!scrolling) emit pressed();
53 scrolling = false;
55 event->ignore();
57 void SLabel::mouseMoveEvent ( QMouseEvent * event )
59 if (scrolling or
60 abs(start_event_x - event->globalX()) > SCROLL_SENSITIVITY or
61 abs(start_event_y - event->globalY()) > SCROLL_SENSITIVITY) {
62 scrolling = true;
64 if (!scrolling) {
65 event->ignore();
66 return;
68 QObject *q = parent();
69 QString s;
70 while (q and (s = q->metaObject()->className()) != "Shopper::ListView"){
71 DEBUG("found " << s);
72 q = q->parent();
74 if (!q) {
75 DEBUG("No Shopper::ListView in parents");
76 return;
78 QScrollArea *qs = qobject_cast<QScrollArea *>(q);
79 int curr_y = event->globalY();
80 int max_y= qApp->desktop()->screenGeometry().height();
81 int s_min = qs->verticalScrollBar()->minimum();
82 int s_max = qs->verticalScrollBar()->maximum();
83 DEBUG(" curr_y["<<curr_y<<"] - start_event_y["<<start_event_y<<"])*2)) * (s_max["<<s_max<<"]-s_min["<<s_min<<"])/max_y["<<max_y<<"] + s_min["<<s_min<<"]");
84 int v = (((curr_y - start_event_y)*2) * (s_max-s_min))/max_y;
85 qs->verticalScrollBar()->setValue(v);
86 DEBUG("Scrolling to " << v);
89 void SLabel::mousePressEvent ( QMouseEvent * event )
91 if (event->button() == Qt::LeftButton){
92 start_event_x = event->globalX();
93 start_event_y = event->globalY();
95 event->ignore();
97 // LabelEntry - a label that turns into a textbox when clicked
98 LabelEntry::LabelEntry(QWidget *parent) :
99 QWidget(parent),
100 data(""),
101 entry(NULL),
102 layout(this)
104 init();
106 LabelEntry::LabelEntry(const QString & value, QWidget *parent) :
107 QWidget(parent),
108 data(value),
109 entry(NULL),
110 layout(this)
112 init();
114 void LabelEntry::init()
116 label = new SLabel(data, this);
117 connect(label, SIGNAL(pressed()),
118 this, SLOT(label_clicked()));
119 layout.addWidget(label);
120 layout.setContentsMargins(0, 0, 0, 0);
121 label->show();
123 QString LabelEntry::getText()
125 return data;
128 void LabelEntry::label_clicked()
130 _ENTER;
131 if (entry != NULL) return; // events may arrive post-creation
132 entry = new QLineEdit(this);
133 entry->setSizePolicy(label->sizePolicy());
134 // entry->set_activates_default(false);
135 // entry->set_width_chars(15); // FIXME
136 entry->setText(data);
137 // self-destruct on unfocus or activate
138 connect(entry, SIGNAL(editingFinished()),
139 this, SLOT(entry_finished()));
141 layout.removeWidget(label);
142 label->hide();
143 layout.addWidget(entry);
144 entry->show();
145 entry->setFocus(Qt::MouseFocusReason);
147 void LabelEntry::entry_finished()
149 _ENTER;
150 if (entry == NULL) return; // events may arrive post-deletion
151 data=entry->text();
152 label->setText(data);
153 layout.removeWidget(entry);
154 entry->hide();
155 layout.addWidget(label);
156 label->show();
157 entry->deleteLater(); // We can now forget about it...
158 entry=NULL;
159 emit changed();
161 void LabelEntry::setAlignment ( Qt::Alignment al )
163 label->setAlignment(al);
165 void LabelEntry::setText ( const QString & d )
167 data = d;
169 void LabelEntry::setVisible(bool vis)
171 _ENTER;
172 QWidget::setVisible(vis);
175 void LabelEntry::setFrameStyle(int i)
177 label->setFrameStyle(i);
179 void LabelEntry::setSizePolicy(QSizePolicy::Policy h, QSizePolicy::Policy v)
181 QWidget::setSizePolicy(h,v);
182 label->setSizePolicy(h,v);
184 void LabelEntry::setSizePolicy(QSizePolicy p)
186 _ENTER;
187 QWidget::setSizePolicy(p);
188 label->setSizePolicy(p);