]> git.sur5r.net Git - minitube/blob - src/regionsview.cpp
b2b41c3afd0c5115ff996be0eece33b94de658bd
[minitube] / src / regionsview.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "regionsview.h"
22 #include "ytregions.h"
23 #include "mainwindow.h"
24
25 RegionsView::RegionsView(QWidget *parent) : View(parent) {
26     QBoxLayout *l = new QVBoxLayout(this);
27     l->setMargin(30);
28     l->setSpacing(30);
29
30     layout = new QGridLayout();
31     layout->setMargin(0);
32     layout->setSpacing(0);
33     l->addLayout(layout);
34
35     addRegion(YTRegions::worldwideRegion());
36     foreach(YTRegion region, YTRegions::list())
37         addRegion(region);
38
39     doneButton = new QPushButton(tr("Done"));
40     doneButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
41     doneButton->setDefault(true);
42 #ifndef APP_MAC
43     doneButton->setProperty("custom", true);
44     doneButton->setProperty("important", true);
45     doneButton->setProperty("big", true);
46 #endif
47     connect(doneButton, SIGNAL(clicked()), MainWindow::instance(), SLOT(goBack()));
48     l->addWidget(doneButton, 0, Qt::AlignHCenter | Qt::AlignTop);
49 }
50
51 void RegionsView::addRegion(const YTRegion &region) {
52     QPushButton *button = new QPushButton(region.name);
53     button->setProperty("regionId", region.id);
54     button->setCheckable(true);
55     button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
56     button->setFocusPolicy(Qt::StrongFocus);
57     button->setIcon(YTRegions::iconForRegionId(region.id));
58     connect(button, SIGNAL(clicked()), SLOT(buttonClicked()));
59     const int i = layout->count();
60     static const int rows = 10;
61     layout->addWidget(button, i % rows, i / rows);
62 }
63
64 void RegionsView::appear() {
65     doneButton->setFocus();
66
67     QString currentRegionId = YTRegions::currentRegionId();
68     for (int i = 0; i < layout->count(); i++) {
69         QLayoutItem *item = layout->itemAt(i);
70         QPushButton *b = static_cast<QPushButton*>(item->widget());
71         QString regionId = b->property("regionId").toString();
72         b->setChecked(currentRegionId == regionId);
73     }
74 }
75
76 void RegionsView::paintEvent(QPaintEvent *e) {
77     QWidget::paintEvent(e);
78     QBrush brush;
79     if (window()->isActiveWindow()) {
80         brush = Qt::white;
81     } else {
82         brush = palette().window();
83     }
84     QPainter painter(this);
85     painter.fillRect(0, 0, width(), height(), brush);
86     painter.end();
87 }
88
89 void RegionsView::buttonClicked() {
90     QObject* o = sender();
91     QString regionId = o->property("regionId").toString();
92     YTRegions::setRegion(regionId);
93     emit regionChanged();
94     doneButton->click();
95
96     // uncheck other buttons
97     /*
98     for (int i = 0; i < layout->count(); i++) {
99         QLayoutItem *item = layout->itemAt(i);
100         QPushButton *b = static_cast<QPushButton*>(item->widget());
101         if (b != o && b->isChecked()) b->setChecked(false);
102     }
103     */
104 }