1 /***
2
3 Olive - Non-Linear Video Editor
4 Copyright (C) 2019 Olive Team
5
6 This program 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 This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
18
19 ***/
20
21 #include "sourceiconview.h"
22
23 #include <QMimeData>
24 #include <QImage>
25
26 #include "panels/project.h"
27 #include "project/media.h"
28 #include "project/sourcescommon.h"
29 #include "global/debug.h"
30 #include "global/math.h"
31
SourceIconView(SourcesCommon & commons)32 SourceIconView::SourceIconView(SourcesCommon &commons) :
33 commons_(commons)
34 {
35 setMovement(QListView::Free);
36 setSelectionMode(QAbstractItemView::ExtendedSelection);
37 setResizeMode(QListView::Adjust);
38 setContextMenuPolicy(Qt::CustomContextMenu);
39 setItemDelegate(&delegate_);
40 connect(this, SIGNAL(clicked(const QModelIndex&)), this, SLOT(item_click(const QModelIndex&)));
41 connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(show_context_menu()));
42 }
43
show_context_menu()44 void SourceIconView::show_context_menu() {
45 commons_.show_context_menu(this, selectedIndexes());
46 }
47
item_click(const QModelIndex & index)48 void SourceIconView::item_click(const QModelIndex& index) {
49 if (selectedIndexes().size() == 1 && index.column() == 0) {
50 commons_.item_click(project_parent->item_to_media(index), index);
51 }
52 }
53
mousePressEvent(QMouseEvent * event)54 void SourceIconView::mousePressEvent(QMouseEvent* event) {
55 commons_.mousePressEvent(event);
56 if (!indexAt(event->pos()).isValid()) selectionModel()->clear();
57 QListView::mousePressEvent(event);
58 }
59
dragEnterEvent(QDragEnterEvent * event)60 void SourceIconView::dragEnterEvent(QDragEnterEvent *event) {
61 if (event->mimeData()->hasUrls()) {
62 event->acceptProposedAction();
63 } else {
64 QListView::dragEnterEvent(event);
65 }
66 }
67
dragMoveEvent(QDragMoveEvent * event)68 void SourceIconView::dragMoveEvent(QDragMoveEvent *event) {
69 if (event->mimeData()->hasUrls()) {
70 event->acceptProposedAction();
71 } else {
72 QListView::dragMoveEvent(event);
73 }
74 }
75
dropEvent(QDropEvent * event)76 void SourceIconView::dropEvent(QDropEvent* event) {
77 QModelIndex drop_item = indexAt(event->pos());
78 if (!drop_item.isValid()) drop_item = rootIndex();
79 commons_.dropEvent(this, event, drop_item, selectedIndexes());
80 }
81
mouseDoubleClickEvent(QMouseEvent *)82 void SourceIconView::mouseDoubleClickEvent(QMouseEvent *) {
83 if (selectedIndexes().size() == 1) {
84 Media* m = project_parent->item_to_media(selectedIndexes().at(0));
85 if (m->get_type() == MEDIA_TYPE_FOLDER) {
86 setRootIndex(selectedIndexes().at(0));
87 emit changed_root();
88 return;
89 }
90 }
91
92 // Double click was not a folder, so we perform the default behavior (sending the double click to SourcesCommon)
93 commons_.mouseDoubleClickEvent(selectedIndexes());
94 }
95
SourceIconDelegate(QObject * parent)96 SourceIconDelegate::SourceIconDelegate(QObject *parent) :
97 QStyledItemDelegate (parent)
98 {
99 }
100
sizeHint(const QStyleOptionViewItem & option,const QModelIndex &) const101 QSize SourceIconDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &) const
102 {
103 if (option.decorationPosition == QStyleOptionViewItem::Top) { // Icon Mode
104
105 return QSize(256, 256);
106
107 } else {
108
109 return QSize(option.decorationSize.height(), option.decorationSize.height());
110
111 }
112 }
113
paint(QPainter * painter,const QStyleOptionViewItem & option,const QModelIndex & index) const114 void SourceIconDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
115 {
116 QFontMetrics fm = painter->fontMetrics();
117 QRect img_rect = option.rect;
118
119 if (option.decorationPosition == QStyleOptionViewItem::Top) { // Icon Mode
120
121 // Draw Text
122 if (fm.height() < option.rect.height() / 2) {
123 img_rect.setHeight(img_rect.height()-fm.height());
124
125 QRect text_rect = option.rect;
126 text_rect.setTop(text_rect.top() + option.rect.height() - fm.height());
127
128 QColor text_bgcolor;
129 QColor text_fgcolor;
130
131 if (option.state & QStyle::State_Selected) {
132 text_bgcolor = option.palette.highlight().color();
133 text_fgcolor = option.palette.highlightedText().color();
134 } else {
135 text_bgcolor = Qt::white;
136 text_fgcolor = Qt::black;
137 }
138
139 painter->fillRect(text_rect, text_bgcolor);
140 painter->setPen(text_fgcolor);
141
142 QString duration_str = index.data(Qt::UserRole).toString();
143 int timecode_width = fm.width(duration_str);
144 int max_name_width = option.rect.width();
145
146 if (timecode_width < option.rect.width() / 2) {
147 painter->drawText(text_rect, Qt::AlignBottom | Qt::AlignRight, index.data(Qt::UserRole).toString());
148 max_name_width -= timecode_width;
149 }
150
151 painter->drawText(text_rect,
152 Qt::AlignBottom | Qt::AlignLeft,
153 fm.elidedText(index.data(Qt::DisplayRole).toString(), Qt::ElideRight, max_name_width));
154
155 }
156
157 // Draw image
158 QIcon ico = index.data(Qt::DecorationRole).value<QIcon>();
159 QSize icon_size = ico.actualSize(img_rect.size());
160 img_rect = QRect(img_rect.x() + (img_rect.width() / 2 - icon_size.width() / 2),
161 img_rect.y() + (img_rect.height() / 2 - icon_size.height() / 2),
162 icon_size.width(),
163 icon_size.height());
164 painter->drawPixmap(img_rect, ico.pixmap(icon_size));
165
166 if (option.state & QStyle::State_Selected) {
167 QColor highlight_color = option.palette.highlight().color();
168 highlight_color.setAlphaF(0.5);
169
170 painter->setCompositionMode(QPainter::CompositionMode_SourceAtop);
171 painter->fillRect(img_rect, highlight_color);
172 }
173 } else if (option.decorationPosition == QStyleOptionViewItem::Left) { // List Mode
174
175 if (option.state & QStyle::State_Selected) {
176 painter->fillRect(option.rect, option.palette.highlight());
177 }
178
179 img_rect.setWidth(qMin(img_rect.width(), img_rect.height()));
180
181 QIcon ico = index.data(Qt::DecorationRole).value<QIcon>();
182 QSize icon_size = ico.actualSize(img_rect.size());
183 img_rect = QRect(img_rect.x() + (img_rect.width() / 2 - icon_size.width() / 2),
184 img_rect.y() + (img_rect.height() / 2 - icon_size.height() / 2),
185 icon_size.width(),
186 icon_size.height());
187 painter->drawPixmap(img_rect, ico.pixmap(icon_size));
188
189 QRect text_rect = option.rect;
190 text_rect.setLeft(text_rect.left() + option.rect.height());
191
192 int maximum_line_count = qMax(1, option.rect.height() / fm.height() - 1);
193 QString text;
194 if (maximum_line_count == 1) {
195 text = index.data(Qt::DisplayRole).toString();
196 } else {
197 text = index.data(Qt::ToolTipRole).toString();
198 if (text.isEmpty()) {
199 text = index.data(Qt::DisplayRole).toString();
200 } else {
201 QStringList strings = text.split("\n");
202 while (strings.size() > maximum_line_count) {
203 strings.removeLast();
204 }
205 text = strings.join("\n");
206 }
207 }
208
209 painter->setPen(option.state & QStyle::State_Selected ?
210 option.palette.highlightedText().color() : option.palette.text().color());
211
212 painter->drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, text);
213
214 }
215 }
216