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
22 #include "shopper.h" // automake, i8n, gettext
23 #include "ItemDialog.h"
24 #include "LabelEntry.h"
25 #include "NoteWidget.h"
29 #include <QSizePolicy>
32 #include <QContextMenuEvent>
36 #include "GestureWatcher.h"
42 const int ItemView::MINFONTSIZE
= 14;
43 const int ItemView::MAXFONTSIZE
= 28;
44 ItemView::ItemView(Item
&it
, List
&l
, QWidget
*parent
) :
53 bool showTwoColumns
= settings
.value("ui/ShowTwoColumns").toBool();
54 // setStyleSheet("color : green;margin: 0px;border: 0px ;padding: 0px ;");
56 QPalette p
= palette();
57 p
.setColor(QPalette::WindowText
, QColor(0,0,0));
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
);
73 QFont font
= desc
->font();
74 if (mylist
->get_state() == MAKING_LIST
)
75 font
.setStrikeOut(false);
77 font
.setStrikeOut(active
);
81 outer_b
.addWidget(tick
);
82 outer_b
.addLayout(&inner_b
,1);
83 inner_b
.addWidget(desc
, 0);
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));
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);
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()));
129 DEBUG("ItemView created for " <<myitem
->get_desc());
132 // Prepare menu actions at time of popup
133 void ItemView::contextMenuEvent( QContextMenuEvent
* event
)
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());
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);
156 if ((mylist
->get_state() == WHATS_LEFT
) && myitem
->get_bought()) {
157 QWidget::setVisible(false);
160 QWidget::setVisible(vis
);
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
);
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
191 DEBUG("Tick " << active
<<"\n");
192 switch (mylist
->get_state()) {
196 DEBUG("what's left\n");
197 myitem
->set_bought(active
);
200 DEBUG("Making list\n");
201 myitem
->set_wanted(active
);
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);
219 font
.setStrikeOut(active
);
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())))
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
);
243 if (note
) note
->setFont(font
);