Changeset View
Changeset View
Standalone View
Standalone View
src/lib/bookmarks/bookmarkstoolbarbutton.cpp
Show All 11 Lines | |||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | 13 | * GNU General Public License for more details. | ||
14 | * | 14 | * | ||
15 | * You should have received a copy of the GNU General Public License | 15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | * ============================================================ */ | 17 | * ============================================================ */ | ||
18 | #include "bookmarkstoolbarbutton.h" | 18 | #include "bookmarkstoolbarbutton.h" | ||
19 | #include "bookmarkstools.h" | 19 | #include "bookmarkstools.h" | ||
20 | #include "bookmarksmodel.h" | ||||
20 | #include "bookmarkitem.h" | 21 | #include "bookmarkitem.h" | ||
21 | #include "bookmarks.h" | 22 | #include "bookmarks.h" | ||
22 | #include "mainapplication.h" | 23 | #include "mainapplication.h" | ||
23 | #include "enhancedmenu.h" | 24 | #include "enhancedmenu.h" | ||
24 | 25 | | |||
25 | #include <QStyle> | 26 | #include <QStyle> | ||
26 | #include <QPainter> | 27 | #include <QPainter> | ||
27 | #include <QMouseEvent> | 28 | #include <QMouseEvent> | ||
28 | #include <QStyleOptionButton> | 29 | #include <QStyleOptionButton> | ||
30 | #include <QDrag> | ||||
31 | #include <QMimeData> | ||||
29 | 32 | | |||
30 | #define MAX_WIDTH 150 | 33 | #define MAX_WIDTH 150 | ||
31 | #define SEPARATOR_WIDTH 8 | 34 | #define SEPARATOR_WIDTH 8 | ||
32 | #define PADDING 5 | 35 | #define PADDING 5 | ||
33 | 36 | | |||
34 | BookmarksToolbarButton::BookmarksToolbarButton(BookmarkItem* bookmark, QWidget* parent) | 37 | BookmarksToolbarButton::BookmarksToolbarButton(BookmarkItem* bookmark, QWidget* parent) | ||
35 | : QPushButton(parent) | 38 | : QPushButton(parent) | ||
36 | , m_bookmark(bookmark) | 39 | , m_bookmark(bookmark) | ||
37 | , m_window(0) | 40 | , m_window(0) | ||
38 | , m_showOnlyIcon(false) | 41 | , m_showOnlyIcon(false) | ||
39 | { | 42 | { | ||
40 | init(); | 43 | init(); | ||
44 | | ||||
45 | if (m_bookmark->isFolder()) { | ||||
46 | setAcceptDrops(true); | ||||
47 | } | ||||
41 | } | 48 | } | ||
42 | 49 | | |||
43 | BookmarkItem* BookmarksToolbarButton::bookmark() const | 50 | BookmarkItem* BookmarksToolbarButton::bookmark() const | ||
44 | { | 51 | { | ||
45 | return m_bookmark; | 52 | return m_bookmark; | ||
46 | } | 53 | } | ||
47 | 54 | | |||
48 | void BookmarksToolbarButton::setMainWindow(BrowserWindow* window) | 55 | void BookmarksToolbarButton::setMainWindow(BrowserWindow* window) | ||
▲ Show 20 Lines • Show All 212 Lines • ▼ Show 20 Line(s) | |||||
261 | { | 268 | { | ||
262 | if (m_bookmark && m_bookmark->isFolder()) { | 269 | if (m_bookmark && m_bookmark->isFolder()) { | ||
263 | if (event->buttons() == Qt::LeftButton && event->modifiers() == Qt::ControlModifier) { | 270 | if (event->buttons() == Qt::LeftButton && event->modifiers() == Qt::ControlModifier) { | ||
264 | openFolder(m_bookmark); | 271 | openFolder(m_bookmark); | ||
265 | return; | 272 | return; | ||
266 | } | 273 | } | ||
267 | } | 274 | } | ||
268 | 275 | | |||
276 | m_dragStartPosition = event->pos(); | ||||
277 | | ||||
269 | QPushButton::mousePressEvent(event); | 278 | QPushButton::mousePressEvent(event); | ||
270 | } | 279 | } | ||
271 | 280 | | |||
272 | void BookmarksToolbarButton::mouseReleaseEvent(QMouseEvent* event) | 281 | void BookmarksToolbarButton::mouseReleaseEvent(QMouseEvent* event) | ||
273 | { | 282 | { | ||
274 | if (m_bookmark && rect().contains(event->pos())) { | 283 | if (m_bookmark && rect().contains(event->pos())) { | ||
275 | Qt::MouseButton button = event->button(); | 284 | Qt::MouseButton button = event->button(); | ||
276 | Qt::KeyboardModifiers modifiers = event->modifiers(); | 285 | Qt::KeyboardModifiers modifiers = event->modifiers(); | ||
Show All 12 Lines | |||||
289 | else if (m_bookmark->isFolder() && button == Qt::MiddleButton) { | 298 | else if (m_bookmark->isFolder() && button == Qt::MiddleButton) { | ||
290 | openFolder(m_bookmark); | 299 | openFolder(m_bookmark); | ||
291 | } | 300 | } | ||
292 | } | 301 | } | ||
293 | 302 | | |||
294 | QPushButton::mouseReleaseEvent(event); | 303 | QPushButton::mouseReleaseEvent(event); | ||
295 | } | 304 | } | ||
296 | 305 | | |||
306 | void BookmarksToolbarButton::mouseMoveEvent(QMouseEvent *event) | ||||
307 | { | ||||
308 | if ((event->pos() - m_dragStartPosition).manhattanLength() < QApplication::startDragDistance()) { | ||||
309 | QPushButton::mouseMoveEvent(event); | ||||
310 | return; | ||||
311 | } | ||||
drosca: `QDrag *drag` | |||||
312 | | ||||
313 | setDown(false); | ||||
314 | | ||||
315 | QDrag *drag = new QDrag(this); | ||||
316 | BookmarksButtonMimeData* mime = new BookmarksButtonMimeData; | ||||
drosca: You don't need to store that drag is in progress as drags are sync. | |||||
317 | mime->setBookmarkItem(m_bookmark); | ||||
318 | drag->setMimeData(mime); | ||||
319 | drag->setPixmap(grab()); | ||||
This still needs to call mouseMoveEvent of parent class. if ((event->pos() - m_dragStartPosition).manhattanLength() < QApplication::startDragDistance()) { QPushButton::mouseMoveEvent(event); return; } drosca: This still needs to call mouseMoveEvent of parent class.
```
if ((event->pos()… | |||||
320 | drag->exec(); | ||||
321 | } | ||||
322 | | ||||
297 | void BookmarksToolbarButton::paintEvent(QPaintEvent* event) | 323 | void BookmarksToolbarButton::paintEvent(QPaintEvent* event) | ||
298 | { | 324 | { | ||
299 | Q_UNUSED(event) | 325 | Q_UNUSED(event) | ||
300 | 326 | | |||
301 | QPainter p(this); | 327 | QPainter p(this); | ||
How does dragging between windows work? It should use custom mimedata with BookmarkItem pointer (see TabModel). drosca: How does dragging between windows work?
It should use custom mimedata with BookmarkItem… | |||||
302 | 328 | | |||
303 | // Just draw separator | 329 | // Just draw separator | ||
304 | if (m_bookmark->isSeparator()) { | 330 | if (m_bookmark->isSeparator()) { | ||
305 | QStyleOption opt; | 331 | QStyleOption opt; | ||
It starts drag on every mouse move event (with pressed mouse button)? Why is it calculating start drag distance then? drosca: It starts drag on every mouse move event (with pressed mouse button)? Why is it calculating… | |||||
306 | opt.initFrom(this); | 332 | opt.initFrom(this); | ||
307 | opt.state |= QStyle::State_Horizontal; | 333 | opt.state |= QStyle::State_Horizontal; | ||
308 | style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &opt, &p); | 334 | style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &opt, &p); | ||
309 | return; | 335 | return; | ||
310 | } | 336 | } | ||
311 | 337 | | |||
312 | QStyleOptionButton option; | 338 | QStyleOptionButton option; | ||
313 | initStyleOption(&option); | 339 | initStyleOption(&option); | ||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Line(s) | 382 | if (!m_showOnlyIcon) { | |||
357 | const int textWidth = rightPosition - leftPosition; | 383 | const int textWidth = rightPosition - leftPosition; | ||
358 | const int textYPos = center - fontMetrics().height() / 2; | 384 | const int textYPos = center - fontMetrics().height() / 2; | ||
359 | const QString txt = fontMetrics().elidedText(m_bookmark->title(), Qt::ElideRight, textWidth); | 385 | const QString txt = fontMetrics().elidedText(m_bookmark->title(), Qt::ElideRight, textWidth); | ||
360 | QRect textRect(leftPosition, textYPos, textWidth, fontMetrics().height()); | 386 | QRect textRect(leftPosition, textYPos, textWidth, fontMetrics().height()); | ||
361 | style()->drawItemText(&p, QStyle::visualRect(option.direction, option.rect, textRect), | 387 | style()->drawItemText(&p, QStyle::visualRect(option.direction, option.rect, textRect), | ||
362 | Qt::TextSingleLine | Qt::AlignCenter, option.palette, true, txt); | 388 | Qt::TextSingleLine | Qt::AlignCenter, option.palette, true, txt); | ||
363 | } | 389 | } | ||
364 | } | 390 | } | ||
391 | | ||||
392 | void BookmarksToolbarButton::dragEnterEvent(QDragEnterEvent *event) | ||||
393 | { | ||||
394 | const QMimeData* mime = event->mimeData(); | ||||
395 | if ((mime->hasUrls() && mime->hasText()) || mime->hasFormat(BookmarksButtonMimeData::mimeType())) { | ||||
396 | event->acceptProposedAction(); | ||||
397 | setDown(true); | ||||
398 | return; | ||||
399 | } | ||||
400 | | ||||
401 | QPushButton::dragEnterEvent(event); | ||||
402 | } | ||||
403 | | ||||
404 | void BookmarksToolbarButton::dragLeaveEvent(QDragLeaveEvent *event) | ||||
405 | { | ||||
406 | Q_UNUSED(event); | ||||
407 | setDown(false); | ||||
408 | } | ||||
409 | | ||||
410 | void BookmarksToolbarButton::dropEvent(QDropEvent *event) | ||||
411 | { | ||||
412 | setDown(false); | ||||
413 | | ||||
414 | const QMimeData* mime = event->mimeData(); | ||||
415 | if (!mime->hasUrls() && !mime->hasFormat(BookmarksButtonMimeData::mimeType())) { | ||||
416 | QPushButton::dropEvent(event); | ||||
417 | return; | ||||
418 | } | ||||
419 | | ||||
420 | BookmarkItem* bookmark = nullptr; | ||||
421 | | ||||
422 | if (mime->hasFormat(BookmarksButtonMimeData::mimeType())) { | ||||
423 | const BookmarksButtonMimeData* bookmarkMime = static_cast<const BookmarksButtonMimeData*>(mime); | ||||
424 | bookmark = bookmarkMime->item(); | ||||
425 | } else { | ||||
426 | const QUrl url = mime->urls().at(0); | ||||
427 | const QString title = mime->hasText() ? mime->text() : url.toEncoded(QUrl::RemoveScheme); | ||||
428 | | ||||
429 | bookmark = new BookmarkItem(BookmarkItem::Url); | ||||
430 | bookmark->setTitle(title); | ||||
431 | bookmark->setUrl(url); | ||||
432 | } | ||||
433 | | ||||
434 | mApp->bookmarks()->addBookmark(m_bookmark, bookmark); | ||||
435 | } |
QDrag *drag