| 1 | /* |
| 2 | * Copyright 2014 Mihai Niculescu <[email protected]> |
| 3 | * |
| 4 | * This file is part of EqualX Project (https://launchpad.net/equalx/) |
| 5 | * |
| 6 | * EqualX 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 | * EqualX 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 | #include <QDebug> |
| 21 | #include <QDragEnterEvent> |
| 22 | #include <QMessageBox> |
| 23 | #include <QMenu> |
| 24 | #include <QMimeData> |
| 25 | #include <QStandardItemModel> |
| 26 | #include <QUrl> |
| 27 | |
| 28 | #include "File.h" |
| 29 | #include "FileInfo.h" |
| 30 | #include "Library/Library.h" |
| 31 | |
| 32 | #include "BookmarksPanel/BookmarkItem.h" |
| 33 | #include "BookmarksPanel/BookmarksItemModel.h" |
| 34 | #include "BookmarksPanel/BookmarksViewItemDelegate.h" |
| 35 | #include "BookmarksPanel/BookmarksWidget.h" |
| 36 | #include "BookmarksPanel/DialogPreferencesFolder.h" |
| 37 | #include "BookmarksPanel/DialogPreferencesBookmark.h" |
| 38 | |
| 39 | #include "ui_BookmarksWidget.h" |
| 40 | |
| 41 | BookmarksWidget::BookmarksWidget(QWidget *parent) : |
| 42 | QWidget(parent), |
| 43 | ui(new Ui::BookmarksWidget), |
| 44 | mCopiedRow(), |
| 45 | mIsCutActive(false) |
| 46 | { |
| 47 | ui->setupUi(this); |
| 48 | |
| 49 | ui->widgetSearchImage->setHidden(true); |
| 50 | |
| 51 | mDgPreferencesFolder = new DialogPreferencesFolder(this); |
| 52 | mDgPreferencesBookmark= new DialogPreferencesBookmark(this); |
| 53 | |
| 54 | // Bookmarks View Page |
| 55 | mModel = new BookmarksItemModel(LibraryManager::Instance()); |
| 56 | |
| 57 | BookmarksViewItemDelegate *bviDelegate = new BookmarksViewItemDelegate(this); |
| 58 | |
| 59 | ui->treeView->setItemDelegate(bviDelegate); |
| 60 | ui->treeView->setAlternatingRowColors(true); |
| 61 | //ui->treeView->setHeaderHidden(true); |
| 62 | ui->treeView->setContextMenuPolicy(Qt::ActionsContextMenu); |
| 63 | ui->treeView->setSelectionMode(QAbstractItemView::SingleSelection); |
| 64 | ui->treeView->setDragEnabled(true); |
| 65 | ui->treeView->setAcceptDrops(true); |
| 66 | ui->treeView->setDropIndicatorShown(true); |
| 67 | ui->treeView->setDragDropMode(QAbstractItemView::InternalMove); |
| 68 | ui->treeView->setExpandsOnDoubleClick ( true ); |
| 69 | ui->treeView->setItemsExpandable(true); |
| 70 | |
| 71 | ui->treeView->setModel(mModel); |
| 72 | |
| 73 | // Search Page |
| 74 | mSearchModel = new QStandardItemModel(this); |
| 75 | BookmarksViewItemDelegate *bviDelegate2 = new BookmarksViewItemDelegate(this); |
| 76 | ui->listView->setItemDelegate(bviDelegate2); |
| 77 | ui->listView->setModel(mSearchModel); |
| 78 | |
| 79 | |
| 80 | setWindowTitle("Bookmarks"); |
| 81 | |
| 82 | setupContextMenuActions(); |
| 83 | setupBookmarksSearchFields(); |
| 84 | |
| 85 | connect(ui->treeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(onDoubleClick(QModelIndex)) ); |
| 86 | connect(ui->listView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(onDoubleClick(QModelIndex)) ); |
| 87 | connect(ui->bookmarkSearchEdit, SIGNAL(textChanged(QString)), this, SLOT(resetAutoSearchTimer(QString)) ); |
| 88 | connect(&mAutoSearchTimer, SIGNAL(timeout()), this, SLOT(searchBookmarks()) ); |
| 89 | |
| 90 | |
| 91 | setAcceptDrops(true); |
| 92 | } |
| 93 | |
| 94 | BookmarksWidget::~BookmarksWidget() |
| 95 | { |
| 96 | delete ui; |
| 97 | mModel->deleteLater(); |
| 98 | } |
| 99 | |
| 100 | void BookmarksWidget::setupContextMenuActions() |
| 101 | { |
| 102 | QAction* actOpen = new QAction("Open", ui->treeView); |
| 103 | ui->treeView->addAction(actOpen); |
| 104 | |
| 105 | QAction* actSep1 = new QAction(ui->treeView); |
| 106 | actSep1->setSeparator(true); |
| 107 | ui->treeView->addAction(actSep1); |
| 108 | |
| 109 | QAction* actNewFolder = new QAction("New Folder", ui->treeView); |
| 110 | ui->treeView->addAction(actNewFolder); |
| 111 | |
| 112 | QAction* actSep2 = new QAction(ui->treeView); |
| 113 | actSep2->setSeparator(true); |
| 114 | ui->treeView->addAction(actSep2); |
| 115 | |
| 116 | QAction* actCut = new QAction("Cut", ui->treeView); |
| 117 | ui->treeView->addAction(actCut); |
| 118 | |
| 119 | QAction* actCopy = new QAction("Copy", ui->treeView); |
| 120 | actCopy->setShortcut(Qt::CTRL | Qt::Key_C); |
| 121 | ui->treeView->addAction(actCopy); |
| 122 | |
| 123 | QAction* actPaste = new QAction("Paste", ui->treeView); |
| 124 | ui->treeView->addAction(actPaste); |
| 125 | |
| 126 | QAction* actSep3 = new QAction(ui->treeView); |
| 127 | actSep3->setSeparator(true); |
| 128 | ui->treeView->addAction(actSep3); |
| 129 | |
| 130 | QAction* actDelete = new QAction("Delete", ui->treeView); |
| 131 | ui->treeView->addAction(actDelete); |
| 132 | |
| 133 | |
| 134 | QAction* actSep4 = new QAction(ui->treeView); |
| 135 | actSep4->setSeparator(true); |
| 136 | ui->treeView->addAction(actSep4); |
| 137 | |
| 138 | QAction* actProp = new QAction("Properties", ui->treeView); |
| 139 | ui->treeView->addAction(actProp); |
| 140 | |
| 141 | connect(actOpen, SIGNAL(triggered()), this, SLOT(onOpen()) ); |
| 142 | connect(actNewFolder, SIGNAL(triggered()), this, SLOT(onNewFolder()) ); |
| 143 | connect(actCut, SIGNAL(triggered()), this, SLOT(onCut()) ); |
| 144 | connect(actCopy, SIGNAL(triggered()), this, SLOT(onCopy()) ); |
| 145 | connect(actPaste, SIGNAL(triggered()), this, SLOT(onPaste()) ); |
| 146 | connect(actDelete, SIGNAL(triggered()), this, SLOT(onDelete()) ); |
| 147 | connect(actProp, SIGNAL(triggered()), this, SLOT(onProperties()) ); |
| 148 | |
| 149 | } |
| 150 | |
| 151 | void BookmarksWidget::setupBookmarksSearchFields() |
| 152 | { |
| 153 | ui->bookmarkSearchByButton->setPopupMode(QToolButton::InstantPopup); |
| 154 | |
| 155 | QMenu *searchByMenu = new QMenu(ui->bookmarkSearchByButton); |
| 156 | |
| 157 | QAction * actSearchByTD = searchByMenu->addAction(tr("By Title and Description")); |
| 158 | actSearchByTD->setData(LibraryManager::SEARCH_BY_TITLE_DESCRIPTION); |
| 159 | actSearchByTD->setCheckable(true); |
| 160 | |
| 161 | QAction * actSearchByT = searchByMenu->addAction(tr("By Title") ); |
| 162 | actSearchByT->setData(LibraryManager::SEARCH_BY_TITLE); |
| 163 | actSearchByT->setCheckable(true); |
| 164 | |
| 165 | QAction * actSearchByD = searchByMenu->addAction(tr("By Description") ); |
| 166 | actSearchByD->setData(LibraryManager::SEARCH_BY_DESCRIPTION); |
| 167 | actSearchByD->setCheckable(true); |
| 168 | |
| 169 | mActionSearchByContent = searchByMenu->addAction(tr("By Content") ); |
| 170 | mActionSearchByContent->setData(LibraryManager::SEARCH_BY_CONTENT); |
| 171 | mActionSearchByContent->setCheckable(true); |
| 172 | |
| 173 | mActionsGroupSearchBy = new QActionGroup(searchByMenu); |
| 174 | mActionsGroupSearchBy->addAction(actSearchByTD); |
| 175 | mActionsGroupSearchBy->addAction(actSearchByT); |
| 176 | mActionsGroupSearchBy->addAction(actSearchByD); |
| 177 | mActionsGroupSearchBy->addAction(mActionSearchByContent); |
| 178 | |
| 179 | actSearchByTD->setChecked(true); |
| 180 | |
| 181 | connect(mActionsGroupSearchBy, SIGNAL(triggered(QAction*)), this, SLOT(searchBookmarks()) ); |
| 182 | |
| 183 | ui->bookmarkSearchByButton->setMenu(searchByMenu); |
| 184 | |
| 185 | } |
| 186 | /*! This doesnt always work, |
| 187 | * because the dropped files can be SVG, PDF, EPS, TXT |
| 188 | * |
| 189 | void BookmarksWidget::dragEnterEvent(QDragEnterEvent *event) |
| 190 | { |
| 191 | if (event->mimeData()->hasUrls()){ |
| 192 | event->acceptProposedAction(); |
| 193 | |
| 194 | ui->labelSearchImage->clear(); |
| 195 | ui->labelSearchImage->setText(tr("Drop Equation Here")); |
| 196 | |
| 197 | ui->widgetSearch->setVisible(false); |
| 198 | ui->widgetSearchImage->setVisible(true); |
| 199 | |
| 200 | |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void BookmarksWidget::dragLeaveEvent(QDragLeaveEvent *event) |
| 205 | { |
| 206 | if(!(ui->labelSearchImage->pixmap()) ){ |
| 207 | ui->widgetSearch->setVisible(true); |
| 208 | ui->widgetSearchImage->setVisible(false); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | void BookmarksWidget::dropEvent(QDropEvent *ev) |
| 213 | { |
| 214 | QUrl droppedUrl = ev->mimeData()->urls().at(0); |
| 215 | |
| 216 | if(!droppedUrl.isValid()) return; |
| 217 | |
| 218 | EqualX::FileInfo eqMetaData; |
| 219 | |
| 220 | QString filePath = droppedUrl.toLocalFile(); |
| 221 | if(!EqualX::File::fetchInfo(filePath, &eqMetaData)){ |
| 222 | qDebug() << "BookmarksWidget::dropEvent() Couldn't read File metadata!"; |
| 223 | ui->labelSearchImage->setText("Not an EqualX equation"); |
| 224 | return; |
| 225 | } |
| 226 | // dropped Url is an EqualX file |
| 227 | |
| 228 | // showing the equation |
| 229 | QImage eqImg(filePath); |
| 230 | |
| 231 | eqImg = eqImg.scaled(QSize(134,134), Qt::KeepAspectRatio, Qt::SmoothTransformation); |
| 232 | |
| 233 | ui->labelSearchImage->setPixmap(QPixmap::fromImage(eqImg)); |
| 234 | |
| 235 | // searching bookmarks |
| 236 | ui->bookmarkSearchEdit->setText(eqMetaData.equation()); |
| 237 | mActionSearchByContent->setChecked(true); |
| 238 | // signals are emitted and handled in searchBookmarks(); |
| 239 | } |
| 240 | */ |
| 241 | |
| 242 | void BookmarksWidget::onDoubleClick(const QModelIndex& index) |
| 243 | { |
| 244 | LibraryModelData curRow = qvariant_cast<LibraryModelData>(index.data()); |
| 245 | |
| 246 | if(curRow.isBookmark()){ |
| 247 | emit activated(curRow.id); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void BookmarksWidget::onOpen() |
| 252 | { |
| 253 | QModelIndex selected = ui->treeView->selectionModel()->currentIndex(); |
| 254 | if(!selected.isValid()) |
| 255 | return; |
| 256 | |
| 257 | LibraryModelData curRow = qvariant_cast<LibraryModelData>(selected.data()); |
| 258 | |
| 259 | if(curRow.isFolder()){ |
| 260 | ui->treeView->setExpanded(selected, !ui->treeView->isExpanded(selected)); |
| 261 | } |
| 262 | |
| 263 | if(curRow.isBookmark()){ |
| 264 | emit activated(curRow.id); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | void BookmarksWidget::onNewFolder() |
| 269 | { |
| 270 | LibraryManager::Instance()->addBookmarkFolder(tr("New Folder")); |
| 271 | } |
| 272 | |
| 273 | void BookmarksWidget::onCut() |
| 274 | { |
| 275 | onCopy(); |
| 276 | mIsCutActive = true; |
| 277 | } |
| 278 | |
| 279 | void BookmarksWidget::onCopy() |
| 280 | { |
| 281 | mIsCutActive = false; |
| 282 | |
| 283 | qDebug() << "onCopy()"; |
| 284 | |
| 285 | QModelIndex selected = ui->treeView->selectionModel()->currentIndex(); |
| 286 | if(!selected.isValid()) |
| 287 | return; |
| 288 | |
| 289 | mCopiedRow = selected; |
| 290 | } |
| 291 | |
| 292 | void BookmarksWidget::onPaste() |
| 293 | { |
| 294 | qDebug() << "onPaste()"; |
| 295 | |
| 296 | //const QModelIndexList indexList = ui->treeView->selectionModel()->selectedIndexes(); |
| 297 | QModelIndex selected = ui->treeView->selectionModel()->currentIndex(); |
| 298 | if(!selected.isValid() || !mCopiedRow.isValid()) |
| 299 | return; |
| 300 | |
| 301 | qDebug() << "BookmarksItemModel::copy()"; |
| 302 | |
| 303 | BookmarkItem* item = mModel->itemFromIndex(mCopiedRow); |
| 304 | BookmarkItem* parentItem = mModel->itemFromIndex(selected); |
| 305 | |
| 306 | if(!item || !parentItem) |
| 307 | return; |
| 308 | |
| 309 | int itemId = item->data().id; |
| 310 | int parentId = parentItem->data().id; |
| 311 | |
| 312 | switch(item->data().type){ |
| 313 | case LibraryModelData::TypeFolder:{ |
| 314 | LibraryManager::Instance()->copyBookmarkFolder(itemId, parentId); |
| 315 | if(mIsCutActive){ |
| 316 | LibraryManager::Instance()->removeBookmarkFolder(itemId); |
| 317 | mIsCutActive = false; |
| 318 | } |
| 319 | break; |
| 320 | } |
| 321 | case LibraryModelData::TypeBookmark:{ |
| 322 | LibraryManager::Instance()->copyBookmark(itemId, parentId); |
| 323 | if(mIsCutActive){ |
| 324 | LibraryManager::Instance()->removeBookmark(itemId); |
| 325 | mIsCutActive = false; |
| 326 | } |
| 327 | break; |
| 328 | } |
| 329 | default: |
| 330 | break; |
| 331 | } |
| 332 | |
| 333 | } |
| 334 | |
| 335 | void BookmarksWidget::onDelete() |
| 336 | { |
| 337 | QModelIndex index = ui->treeView->selectionModel()->currentIndex(); |
| 338 | if(!index.isValid()) return; |
| 339 | |
| 340 | BookmarkItem* item = mModel->itemFromIndex(index); |
| 341 | |
| 342 | if(item->data().isFolder()) LibraryManager::Instance()->removeBookmarkFolder(item->data().id); |
| 343 | if(item->data().isBookmark()) LibraryManager::Instance()->removeBookmark(item->data().id); |
| 344 | |
| 345 | } |
| 346 | |
| 347 | void BookmarksWidget::onProperties() |
| 348 | { |
| 349 | QModelIndex selected = ui->treeView->selectionModel()->currentIndex(); |
| 350 | if(!selected.isValid()) |
| 351 | return; |
| 352 | |
| 353 | LibraryModelData itemData = mModel->itemFromIndex(selected)->data(); |
| 354 | |
| 355 | if(itemData.isFolder()){ |
| 356 | BookmarkFolder folder = LibraryManager::Instance()->getBookmarkFolder(itemData.id); |
| 357 | |
| 358 | mDgPreferencesFolder->setName(folder.name); |
| 359 | mDgPreferencesFolder->setDescription(folder.description); |
| 360 | int status = mDgPreferencesFolder->exec(); |
| 361 | |
| 362 | if(status==QDialog::Accepted){ |
| 363 | folder.name = mDgPreferencesFolder->name(); |
| 364 | folder.description = mDgPreferencesFolder->description(); |
| 365 | |
| 366 | LibraryManager::Instance()->updateBookmarkFolder(folder); |
| 367 | } |
| 368 | |
| 369 | } else if(itemData.isBookmark()){ |
| 370 | Bookmark b = LibraryManager::Instance()->getBookmark(itemData.id); |
| 371 | |
| 372 | mDgPreferencesBookmark->setTitle(b.title); |
| 373 | mDgPreferencesBookmark->setDescrition(b.description); |
| 374 | mDgPreferencesBookmark->setParentFolder(b.idparent); |
| 375 | |
| 376 | if(mDgPreferencesBookmark->exec()==QDialog::Accepted){ |
| 377 | int parentId = mDgPreferencesBookmark->parentFolder(); |
| 378 | |
| 379 | b.title = mDgPreferencesBookmark->title(); |
| 380 | b.description=mDgPreferencesBookmark->description(); |
| 381 | |
| 382 | LibraryManager::Instance()->updateBookmark(b); |
| 383 | |
| 384 | if(parentId != b.idparent){ // if parent changed |
| 385 | LibraryManager::Instance()->moveBookmark(b.id, parentId); |
| 386 | b.idparent = parentId; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | void BookmarksWidget::resetAutoSearchTimer(const QString &/*text*/) |
| 394 | { |
| 395 | mAutoSearchTimer.start(400); |
| 396 | } |
| 397 | |
| 398 | void BookmarksWidget::searchBookmarks() |
| 399 | { |
| 400 | mAutoSearchTimer.stop(); |
| 401 | |
| 402 | QString searchText = ui->bookmarkSearchEdit->text(); |
| 403 | qDebug() << "Searching bookmarks for:"<<searchText; |
| 404 | if(searchText.isEmpty() || searchText.isNull()){ |
| 405 | ui->stackedWidget->setCurrentIndex(0); |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | mSearchModel->clear(); |
| 410 | ui->stackedWidget->setCurrentIndex(1); |
| 411 | |
| 412 | LibraryManager::SEARCH_MODE searchMode = (LibraryManager::SEARCH_MODE)mActionsGroupSearchBy->checkedAction()->data().toInt(); |
| 413 | |
| 414 | LibraryRowsList rows = LibraryManager::Instance()->searchBookmarks(searchText, searchMode); |
| 415 | |
| 416 | if(rows.isEmpty()){ |
| 417 | //ui->stackedWidget->setCurrentIndex(0); |
| 418 | |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | foreach(LibraryModelData row, rows){ |
| 423 | QStandardItem* item = new QStandardItem(); |
| 424 | item->setData(row, Qt::DisplayRole); |
| 425 | |
| 426 | mSearchModel->appendRow(item); |
| 427 | } |
| 428 | } |