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
20 #define DEBUG_SHOPPER 1
21 #include "shopper.h" // automake, i8n, gettext
22 #include "LabelEntry.h"
25 #define SCROLL_SENSITIVITY 20
28 // SLabel a label that emits pressed() when clicked
29 SLabel::SLabel(QWidget
* parent
, Qt::WindowFlags f
) :
36 SLabel::SLabel( const QString
& text
, QWidget
* parent
, Qt::WindowFlags f
) :
37 QLabel(text
, parent
, f
),
43 void SLabel::setVisible(bool vis
)
46 QLabel::setVisible(vis
);
48 void SLabel::mouseReleaseEvent ( QMouseEvent
* event
)
50 if (event
->button() == Qt::LeftButton
) {
52 if (!scrolling
) emit
pressed();
57 void SLabel::mouseMoveEvent ( QMouseEvent
* event
)
60 abs(start_event_x
- event
->globalX()) > SCROLL_SENSITIVITY
or
61 abs(start_event_y
- event
->globalY()) > SCROLL_SENSITIVITY
) {
68 QObject
*q
= parent();
70 while (q
and (s
= q
->metaObject()->className()) != "Shopper::ListView"){
75 DEBUG("No Shopper::ListView in parents");
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();
97 // LabelEntry - a label that turns into a textbox when clicked
98 LabelEntry::LabelEntry(QWidget
*parent
) :
106 LabelEntry::LabelEntry(const QString
& value
, QWidget
*parent
) :
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);
123 QString
LabelEntry::getText()
128 void LabelEntry::label_clicked()
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
);
143 layout
.addWidget(entry
);
145 entry
->setFocus(Qt::MouseFocusReason
);
147 void LabelEntry::entry_finished()
150 if (entry
== NULL
) return; // events may arrive post-deletion
152 label
->setText(data
);
153 layout
.removeWidget(entry
);
155 layout
.addWidget(label
);
157 entry
->deleteLater(); // We can now forget about it...
161 void LabelEntry::setAlignment ( Qt::Alignment al
)
163 label
->setAlignment(al
);
165 void LabelEntry::setText ( const QString
& d
)
169 void LabelEntry::setVisible(bool vis
)
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
)
187 QWidget::setSizePolicy(p
);
188 label
->setSizePolicy(p
);