Move from SLabel to ActiveLabel
[shopper.git] / src / ui / ItemView.cc
blob611f2168a0c825016c17d361739597fef8ed0301
1 /* Shopper
2 * Copyright (C) 2008 David Greaves <david@dgreaves.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 //#define DEBUG_SHOPPER 1
21 #include "ItemView.h"
22 #include "shopper.h" // automake, i8n, gettext
23 #include "ItemDialog.h"
24 #include "LabelEntry.h"
25 #include "NoteWidget.h"
26 #include <QSettings>
27 #include <QPalette>
28 #include <QColor>
29 #include <QSizePolicy>
30 #include <QCheckBox>
31 #include <QFrame>
32 #include <QContextMenuEvent>
33 #include <QMenu>
34 #include <QFont>
35 #include <iostream>
36 #include "GestureWatcher.h"
38 using namespace std;
40 namespace Shopper
42 const int ItemView::MINFONTSIZE = 14;
43 const int ItemView::MAXFONTSIZE = 28;
44 ItemView::ItemView(Item &it, List &l, QWidget *parent) :
45 QWidget(parent),
46 outer_b(),
47 inner_b(),
48 note(0),
49 myitem(&it),
50 mylist(&l)
52 QSettings settings;
53 bool showTwoColumns = settings.value("ui/ShowTwoColumns").toBool();
54 // setStyleSheet("color : green;margin: 0px;border: 0px ;padding: 0px ;");
55 // setMargin(0);
56 QPalette p = palette();
57 p.setColor(QPalette::WindowText, QColor(0,0,0));
58 setPalette(p);
60 QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
61 sizePolicy.setHorizontalStretch(0);
62 sizePolicy.setVerticalStretch(0);
63 setSizePolicy(sizePolicy);
65 bool active = (mylist->get_state() == MAKING_LIST) ? myitem->get_wanted() : myitem->get_bought();
66 tick = new QCheckBox(this);
67 // The tick represents either wanted or bought
68 tick->setChecked(active);
69 desc = new ActiveLabel(myitem->get_desc(), this);
70 desc->setAlignment(Qt::AlignLeft);
71 desc->setSizePolicy(sizePolicy);
72 desc->setMargin(0);
73 QFont font = desc->font();
74 if (mylist->get_state() == MAKING_LIST)
75 font.setStrikeOut(false);
76 else
77 font.setStrikeOut(active);
78 desc->setFont(font);
80 // Lay them out
81 outer_b.addWidget(tick);
82 outer_b.addLayout(&inner_b,1);
83 inner_b.addWidget(desc, 0);
85 // maybe show a note
86 if (! showTwoColumns) {
87 note = new NoteWidget(myitem->get_note(),this);
88 note->setAlignment(Qt::AlignLeft);
89 note->setSizePolicy(sizePolicy);
90 p.setColor(QPalette::WindowText, QColor(150,150,150));
91 note->setPalette(p);
92 // note->setStyleSheet("border: 1px solid gray");
93 // note->label()->setMargin(0);
94 // note->label()->setLineWidth(2);
95 // note->label()->setMidLineWidth(1);
96 // note->label()->setFrameStyle(QFrame::Panel | QFrame::Sunken);
98 inner_b.addWidget(note, 1);
99 // Respond to clicking the note
100 connect(note, SIGNAL(changed()),
101 this, SLOT(note_changed()));
104 outer_b.setContentsMargins(0, 0, 0, 0);
105 inner_b.setContentsMargins(0, 0, 0, 0);
106 // and apply
107 setLayout(&outer_b);
109 // Respond to user clicking the checkbox.
110 connect(tick, SIGNAL(stateChanged(int)),
111 this, SLOT(tick_changed(int)));
113 // We gesture over the description to activate it
114 GestureWatcher* gw = GestureWatcher::getGestureWatcher();
115 gw->connect(desc, Gesture("l "),
116 this, SLOT(desc_clicked()));
117 gw->connect(desc, Gesture("r "),
118 this, SLOT(desc_clicked()));
120 // And clicking the description is the same.
121 // connect(desc, SIGNAL(pressed()),
122 // this, SLOT(desc_clicked()));
124 // Now connect the item's changed signal to this object's updater
125 connect(&it, SIGNAL(changed()),
126 this, SLOT(updateVisibility()));
128 show();
129 DEBUG("ItemView created for " <<myitem->get_desc());
132 // Prepare menu actions at time of popup
133 void ItemView::contextMenuEvent( QContextMenuEvent * event )
135 _ENTER;
136 QMenu menu(this);
137 connect(menu.addAction(tr("Edit")),
138 SIGNAL(triggered()), this, SLOT(edit_item()));
139 connect(menu.addAction(tr("Delete")),
140 SIGNAL(triggered()), this, SLOT(delete_item()),
141 Qt::QueuedConnection);
142 menu.exec(event->globalPos());
143 event->accept();
146 // Only shows if matching item should be seen.
147 void ItemView::setVisible(bool vis)
149 // Hide if not wanted
150 if (((mylist->get_state() == WHATS_LEFT) || (mylist->get_state() == OUT_SHOPPING))
151 && ! myitem->get_wanted()) {
152 QWidget::setVisible(false);
153 return;
156 if ((mylist->get_state() == WHATS_LEFT) && myitem->get_bought()) {
157 QWidget::setVisible(false);
158 return;
160 QWidget::setVisible(vis);
161 return;
164 void ItemView::note_changed()
166 myitem->set_note(note->text()); // Setting the data will call
167 // an update on the note
169 void ItemView::edit_item()
171 ItemEdit itemD(this, mylist, myitem);
172 itemD.exec();
174 void ItemView::delete_item()
176 DEBUG("Delete Item\n");
177 mylist->rm(*(this->myitem));
179 void ItemView::desc_clicked()
181 // Make the tick tock
182 DEBUG("Desc pressed\n");
183 tick->toggle(); // Invokes tick_changed
184 tick->setFocus(Qt::MouseFocusReason);
186 void ItemView::tick_changed(int s)
188 bool active = (s == Qt::Checked);
189 // Suspend our data watch - we're making the change and don't need
190 // to be notified
191 DEBUG("Tick " << active <<"\n");
192 switch (mylist->get_state()) {
193 case OUT_SHOPPING:
194 DEBUG("shopping ");
195 case WHATS_LEFT:
196 DEBUG("what's left\n");
197 myitem->set_bought(active);
198 break;
199 case MAKING_LIST:
200 DEBUG("Making list\n");
201 myitem->set_wanted(active);
202 break;
203 default:
204 DEBUG("State invalid\n");
208 // When the underlying data changes,
209 void ItemView::updateVisibility()
211 DEBUG("Notified of item change: updating view\n");
212 bool active = (mylist->get_state() == MAKING_LIST) ? myitem->get_wanted() : myitem->get_bought();
214 tick->setChecked(active);
215 QFont font = desc->font();
216 if (mylist->get_state() == MAKING_LIST)
217 font.setStrikeOut(false);
218 else
219 font.setStrikeOut(active);
220 desc->setFont(font);
222 desc->setText(myitem->get_desc());
223 if (note) // may not have a note in 2-column mode
224 note->setText(myitem->get_note());
226 if (! mylist->is_category_active(*(myitem->get_category())))
227 hide();
228 else
229 show();
232 #define BASEFONTSIZE 14
233 #define MINFONTSIZE 14
234 #define MAXFONTSIZE 28
235 void ItemView::setZoom(int z)
237 QFont font = desc->font();
238 int s = BASEFONTSIZE + 2*z;
239 if (s < MINFONTSIZE) s=MINFONTSIZE;
240 if (s > MAXFONTSIZE) s=MAXFONTSIZE;
241 font.setPointSizeF(s);
242 desc->setFont(font);
243 if (note) note->setFont(font);