SelectorDialog replaced with a model/view based system. Improved TextSelectionWidget.
[mebook:mebook.git] / ui / booklistitem.h
1 #ifndef BOOKLISTITEM_H
2 #define BOOKLISTITEM_H
3
4 #include <QWidget>
5 #include <QApplication>
6 #include <QHBoxLayout>
7 #include <QVBoxLayout>
8 #include <QLabel>
9 #include <QFrame>
10 #include <QPainter>
11 #include <QLinearGradient>
12 #include <QAbstractKineticScroller>
13 #include "book.h"
14
15 //TODO:Remove dependancy on Book, class should be independent from others 
16 //Replace with a delegate?
17
18 class BookListItem : public QWidget{
19     Q_OBJECT
20 public:
21     BookListItem(Book *book, QWidget *parent = 0, QAbstractKineticScroller *scrollable = 0) : QWidget(parent){
22         thisBook = book;
23
24         mainLayout = new QHBoxLayout();
25         QMargins margins(0, 0, 0, 0);
26         mainLayout->setContentsMargins(margins);
27         QPixmap temp = book->getCoverImage();
28         if(!temp.isNull()){
29             QLabel *image = new QLabel(this);
30             temp = temp.scaledToWidth(100);
31             image->setPixmap(temp);
32             image->setFixedWidth(temp.width());
33             mainLayout->addWidget(image);
34             mainLayout->setAlignment(image, Qt::AlignLeft | Qt::AlignTop);
35         }
36
37         connect(thisBook, SIGNAL(coverImageAdded()), this, SLOT(coverImageAdded()));
38
39         QVBoxLayout *subLayout = new QVBoxLayout();
40         subLayout->setContentsMargins(margins);
41         mainLayout->addLayout(subLayout);
42
43         QString labelStyleSheet = "QFrame, QLabel{width: 100%;border: 1px solid #eeeeec;border-radius: 8px; background-color: #2e3436;}";
44         QLabel *title = new QLabel(book->getTitle(), this);
45         title->setWordWrap(true);
46 //        title->setStyleSheet(labelStyleSheet);
47
48         subLayout->addWidget(title);
49 //        subLayout->setAlignment(title, Qt::AlignLeft);
50
51         QLabel *author = new QLabel(book->getAuthor(), this);
52         author->setWordWrap(true);
53         author->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
54         subLayout->addWidget(author);
55 //        subLayout->setAlignment(author, Qt::AlignLeft);
56
57         QString subject = book->getSubject();
58         if(!subject.isEmpty() && !subject.isNull()){
59             QLabel *category = new QLabel(subject, this);
60             category->setFont(QFont("Sans", 12));
61             category->setWordWrap(true);
62             category->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
63             subLayout->addWidget(category);
64 //            subLayout->setAlignment(category, Qt::AlignLeft);
65         }
66         subLayout->setSpacing(0);
67         subLayout->addStretch(20);
68
69         QVBoxLayout *superLayout = new QVBoxLayout(this);
70
71         topSeparator = new QFrame(this);
72         topSeparator->setFrameStyle(QFrame::HLine | QFrame::Plain);
73         topSeparator->setLineWidth(1);
74         QPalette topSeparatorPalette = QApplication::palette();
75         topSeparatorPalette.setColor(QPalette::WindowText, QColor(Qt::transparent));
76         topSeparator->setPalette(topSeparatorPalette);
77         superLayout->addWidget(topSeparator);
78
79         superLayout->addLayout(mainLayout);
80
81         separator = new QFrame(this);
82         separator->setFrameStyle(QFrame::HLine | QFrame::Plain);
83         separator->setLineWidth(1);
84         QPalette separatorPalette = QApplication::palette();
85         separatorPalette.setColor(QPalette::WindowText, QColor(Qt::transparent));
86         separator->setPalette(separatorPalette);
87         superLayout->addWidget(separator);
88
89         superLayout->setContentsMargins(margins);
90
91         setLayout(superLayout);
92
93         scroller = scrollable;
94
95 /*        QPalette palette = QApplication::palette();
96         QColor normalbg = palette.window().color();
97         palette.setCurrentColorGroup(QPalette::Active);
98         QColor highlight = palette.highlight().color(); 
99         //highlight.setAlpha(0.5);
100
101         gradient = QLinearGradient(QPointF(0, 0), QPointF(0, height()));
102
103         gradient.setColorAt(0, highlight);
104         gradient.setColorAt(1, highlight);
105
106         setAutoFillBackground(true);*/
107     }
108 protected:
109     void mouseReleaseEvent(QMouseEvent *event){
110         if(scroller){
111             if(scroller->state() == QAbstractKineticScroller::Inactive)
112                 emit selected(thisBook);
113         }
114         QPalette palette = QApplication::palette();
115         palette.setColor(QPalette::WindowText, QColor(Qt::transparent));
116         separator->setPalette(palette);
117         topSeparator->setPalette(palette);
118
119 /*        palette = QApplication::palette();
120         palette.setColor(QPalette::Window, palette.color(QPalette::Button));
121         setPalette(palette);*/
122     }
123     void mousePressEvent(QMouseEvent *event){
124         QPalette palette;
125         palette.setCurrentColorGroup(QPalette::Active);
126         QBrush brush = palette.highlight();
127         palette.setColor(QPalette::WindowText, brush.color());
128         separator->setPalette(palette);
129         topSeparator->setPalette(palette);
130 /*        palette.setBrush(QPalette::Window, QBrush(gradient));
131         setPalette(palette);*/
132     }
133
134 private:
135     Book *thisBook;
136     QAbstractKineticScroller *scroller;
137     QHBoxLayout *mainLayout;
138     QFrame *separator;
139     QFrame *topSeparator;
140     QLinearGradient gradient;
141
142 signals:
143     void selected(Book*);
144
145 public slots:
146     void coverImageAdded(){
147         QPixmap temp = thisBook->getCoverImage();
148         if(!temp.isNull()){
149             QLabel *image = new QLabel(this);
150             temp = temp.scaledToWidth(100);
151             image->setPixmap(temp);
152             image->setFixedWidth(temp.width());
153             mainLayout->insertWidget(0, image);
154             mainLayout->setAlignment(image, Qt::AlignLeft | Qt::AlignTop);
155             adjustSize();
156         }
157     }
158 };
159
160 #endif