| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/feedsview.h" |
| 4 | |
| 5 | #include "3rd-party/boolinq/boolinq.h" |
| 6 | #include "core/feedsmodel.h" |
| 7 | #include "core/feedsproxymodel.h" |
| 8 | #include "definitions/definitions.h" |
| 9 | #include "gui/dialogs/formmain.h" |
| 10 | #include "gui/messagebox.h" |
| 11 | #include "gui/reusable/styleditemdelegatewithoutfocus.h" |
| 12 | #include "miscellaneous/feedreader.h" |
| 13 | #include "miscellaneous/mutex.h" |
| 14 | #include "miscellaneous/settings.h" |
| 15 | #include "miscellaneous/textfactory.h" |
| 16 | #include "services/abstract/feed.h" |
| 17 | #include "services/abstract/gui/formaccountdetails.h" |
| 18 | #include "services/abstract/rootitem.h" |
| 19 | #include "services/abstract/serviceroot.h" |
| 20 | |
| 21 | #include <algorithm> |
| 22 | |
| 23 | #include <QClipboard> |
| 24 | #include <QContextMenuEvent> |
| 25 | #include <QHeaderView> |
| 26 | #include <QMenu> |
| 27 | #include <QPainter> |
| 28 | #include <QPointer> |
| 29 | #include <QTimer> |
| 30 | |
| 31 | FeedsView::FeedsView(QWidget* parent) |
| 32 | : BaseTreeView(parent), m_contextMenuService(nullptr), m_contextMenuBin(nullptr), m_contextMenuCategories(nullptr), |
| 33 | m_contextMenuFeeds(nullptr), m_contextMenuImportant(nullptr), m_contextMenuEmptySpace(nullptr), |
| 34 | m_contextMenuOtherItems(nullptr), m_contextMenuLabel(nullptr), m_contextMenuProbe(nullptr), |
| 35 | m_dontSaveExpandState(false) { |
| 36 | setObjectName(QSL("FeedsView")); |
| 37 | |
| 38 | // Allocate models. |
| 39 | m_sourceModel = qApp->feedReader()->feedsModel(); |
| 40 | m_proxyModel = qApp->feedReader()->feedsProxyModel(); |
| 41 | |
| 42 | m_proxyModel->setView(this); |
| 43 | |
| 44 | // Connections. |
| 45 | connect(&m_expansionDelayer, &QTimer::timeout, this, &FeedsView::reloadDelayedExpansions); |
| 46 | connect(m_sourceModel, &FeedsModel::itemExpandRequested, this, &FeedsView::onItemExpandRequested); |
| 47 | connect(m_sourceModel, &FeedsModel::itemExpandStateSaveRequested, this, &FeedsView::onItemExpandStateSaveRequested); |
| 48 | connect(header(), &QHeaderView::sortIndicatorChanged, this, &FeedsView::saveSortState); |
| 49 | connect(m_proxyModel, |
| 50 | &FeedsProxyModel::requireItemValidationAfterDragDrop, |
| 51 | this, |
| 52 | &FeedsView::validateItemAfterDragDrop); |
| 53 | connect(m_proxyModel, &FeedsProxyModel::indexNotFilteredOutAnymore, this, &FeedsView::reloadItemExpandState); |
| 54 | connect(this, &FeedsView::expanded, this, &FeedsView::onIndexExpanded); |
| 55 | connect(this, &FeedsView::collapsed, this, &FeedsView::onIndexCollapsed); |
| 56 | |
| 57 | setModel(m_proxyModel); |
| 58 | setupAppearance(); |
| 59 | } |
| 60 | |
| 61 | FeedsView::~FeedsView() { |
| 62 | qDebugNN << LOGSEC_GUI << "Destroying FeedsView instance."; |
| 63 | } |
| 64 | |
| 65 | void FeedsView::reloadFontSettings() { |
| 66 | m_sourceModel->setupFonts(); |
| 67 | } |
| 68 | |
| 69 | void FeedsView::setSortingEnabled(bool enable) { |
| 70 | disconnect(header(), &QHeaderView::sortIndicatorChanged, this, &FeedsView::saveSortState); |
| 71 | QTreeView::setSortingEnabled(enable); |
| 72 | connect(header(), &QHeaderView::sortIndicatorChanged, this, &FeedsView::saveSortState); |
| 73 | } |
| 74 | |
| 75 | QList<Feed*> FeedsView::selectedFeeds(bool recursive) const { |
| 76 | auto its = selectedItems(); |
| 77 | QList<Feed*> feeds; |
| 78 | |
| 79 | for (RootItem* it : its) { |
| 80 | feeds.append(it->getSubTreeFeeds(recursive)); |
| 81 | } |
| 82 | |
| 83 | auto std_feeds = boolinq::from(feeds).distinct().toStdList(); |
| 84 | |
| 85 | return FROM_STD_LIST(QList<Feed*>, std_feeds); |
| 86 | } |
| 87 | |
| 88 | RootItem* FeedsView::selectedItem() const { |
| 89 | const QModelIndexList selected_rows = selectionModel()->selectedRows(); |
| 90 | const QModelIndex current_row = currentIndex(); |
| 91 | |
| 92 | if (selected_rows.isEmpty()) { |
| 93 | return nullptr; |
| 94 | } |
| 95 | else { |
| 96 | RootItem* selected_item = m_sourceModel->itemForIndex(m_proxyModel->mapToSource(selected_rows.at(0))); |
| 97 | |
| 98 | if (selected_rows.size() == 1) { |
| 99 | return selected_item; |
| 100 | } |
| 101 | |
| 102 | auto selected_items = boolinq::from(selected_rows) |
| 103 | .select([this](const QModelIndex& idx) { |
| 104 | return m_sourceModel->itemForIndex(m_proxyModel->mapToSource(idx)); |
| 105 | }) |
| 106 | .toStdList(); |
| 107 | |
| 108 | RootItem* current_item = m_sourceModel->itemForIndex(m_proxyModel->mapToSource(current_row)); |
| 109 | |
| 110 | if (std::find(selected_items.begin(), selected_items.end(), current_item) != selected_items.end()) { |
| 111 | return current_item; |
| 112 | } |
| 113 | else { |
| 114 | return selected_items.front(); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | QList<RootItem*> FeedsView::selectedItems() const { |
| 120 | const QModelIndexList selected_rows = selectionModel()->selectedRows(); |
| 121 | |
| 122 | auto selected_items = boolinq::from(selected_rows) |
| 123 | .select([this](const QModelIndex& idx) { |
| 124 | return m_sourceModel->itemForIndex(m_proxyModel->mapToSource(idx)); |
| 125 | }) |
| 126 | .toStdList(); |
| 127 | |
| 128 | return FROM_STD_LIST(QList<RootItem*>, selected_items); |
| 129 | } |
| 130 | |
| 131 | void FeedsView::copyUrlOfSelectedFeeds() const { |
| 132 | auto feeds = selectedFeeds(true); |
| 133 | QStringList urls; |
| 134 | |
| 135 | for (const auto* feed : feeds) { |
| 136 | if (!feed->source().isEmpty()) { |
| 137 | urls << feed->source(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (QGuiApplication::clipboard() != nullptr && !urls.isEmpty()) { |
| 142 | QGuiApplication::clipboard()->setText(urls.join(TextFactory::newline()), QClipboard::Mode::Clipboard); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void FeedsView::sortByColumn(int column, Qt::SortOrder order) { |
| 147 | const int old_column = header()->sortIndicatorSection(); |
| 148 | const Qt::SortOrder old_order = header()->sortIndicatorOrder(); |
| 149 | |
| 150 | if (column == old_column && order == old_order) { |
| 151 | m_proxyModel->sort(column, order); |
| 152 | } |
| 153 | else { |
| 154 | QTreeView::sortByColumn(column, order); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void FeedsView::addFeedIntoSelectedAccount() { |
| 159 | RootItem* selected = selectedItem(); |
| 160 | |
| 161 | if (selected != nullptr) { |
| 162 | ServiceRoot* root = selected->getParentServiceRoot(); |
| 163 | |
| 164 | if (root->supportsFeedAdding()) { |
| 165 | root->addNewFeed(selected, QGuiApplication::clipboard()->text(QClipboard::Mode::Clipboard)); |
| 166 | } |
| 167 | else { |
| 168 | qApp->showGuiMessage(Notification::Event::GeneralEvent, |
| 169 | {tr("Not supported by account"), |
| 170 | tr("Selected account does not support adding of new feeds."), |
| 171 | QSystemTrayIcon::MessageIcon::Warning}); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | void FeedsView::addCategoryIntoSelectedAccount() { |
| 177 | RootItem* selected = selectedItem(); |
| 178 | |
| 179 | if (selected != nullptr) { |
| 180 | ServiceRoot* root = selected->getParentServiceRoot(); |
| 181 | |
| 182 | if (root->supportsCategoryAdding()) { |
| 183 | root->addNewCategory(selected); |
| 184 | } |
| 185 | else { |
| 186 | qApp->showGuiMessage(Notification::Event::GeneralEvent, |
| 187 | {tr("Not supported by account"), |
| 188 | tr("Selected account does not support adding of new categories."), |
| 189 | QSystemTrayIcon::MessageIcon::Warning}); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void FeedsView::expandCollapseCurrentItem(bool recursive) { |
| 195 | if (selectionModel()->selectedRows().size() == 1) { |
| 196 | QModelIndex index = selectionModel()->selectedRows().at(0); |
| 197 | |
| 198 | if (!model()->index(0, 0, index).isValid() && index.parent().isValid()) { |
| 199 | setCurrentIndex(index.parent()); |
| 200 | index = index.parent(); |
| 201 | } |
| 202 | |
| 203 | if (recursive) { |
| 204 | QList<QModelIndex> to_process = {index}; |
| 205 | bool expa = !isExpanded(index); |
| 206 | |
| 207 | while (!to_process.isEmpty()) { |
| 208 | auto idx = to_process.takeFirst(); |
| 209 | |
| 210 | if (idx.isValid()) { |
| 211 | setExpanded(idx, expa); |
| 212 | |
| 213 | for (int i = 0; i < m_proxyModel->rowCount(idx); i++) { |
| 214 | auto new_idx = m_proxyModel->index(i, 0, idx); |
| 215 | |
| 216 | if (new_idx.isValid()) { |
| 217 | to_process << new_idx; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | else { |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | else { |
| 227 | isExpanded(index) ? collapse(index) : expand(index); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void FeedsView::updateSelectedItems() { |
| 233 | auto sel_feeds = selectedFeeds(true); |
| 234 | auto sel_items = selectedItems(); |
| 235 | bool one_specific_feed_selected = sel_items.size() == 1 && sel_items.constFirst()->kind() == RootItem::Kind::Feed; |
| 236 | |
| 237 | qApp->feedReader()->updateFeeds(sel_feeds, one_specific_feed_selected); |
| 238 | } |
| 239 | |
| 240 | void FeedsView::clearSelectedItems() { |
| 241 | if (MsgBox::show(nullptr, |
| 242 | QMessageBox::Icon::Question, |
| 243 | tr("Are you sure?"), |
| 244 | tr("Do you really want to clean all articles from selected items?"), |
| 245 | {}, |
| 246 | {}, |
| 247 | QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No, |
| 248 | QMessageBox::StandardButton::No) != QMessageBox::StandardButton::Yes) { |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | for (auto* it : selectedItems()) { |
| 253 | m_sourceModel->markItemCleared(it, false); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void FeedsView::purgeSelectedFeeds() { |
| 258 | if (MsgBox::show(nullptr, |
| 259 | QMessageBox::Icon::Question, |
| 260 | tr("Are you sure?"), |
| 261 | tr("Do you really want to purge all non-starred articles from selected feeds?"), |
| 262 | {}, |
| 263 | {}, |
| 264 | QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No, |
| 265 | QMessageBox::StandardButton::No) != QMessageBox::StandardButton::Yes) { |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | m_sourceModel->purgeArticles(selectedFeeds(true)); |
| 270 | } |
| 271 | |
| 272 | void FeedsView::clearAllItems() { |
| 273 | if (MsgBox::show(nullptr, |
| 274 | QMessageBox::Icon::Question, |
| 275 | tr("Are you sure?"), |
| 276 | tr("Do you really want to clean all articles from selected items?"), |
| 277 | {}, |
| 278 | {}, |
| 279 | QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No, |
| 280 | QMessageBox::StandardButton::No) != QMessageBox::StandardButton::Yes) { |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | m_sourceModel->markItemCleared(m_sourceModel->rootItem(), false); |
| 285 | } |
| 286 | |
| 287 | void FeedsView::editItems(const QList<RootItem*>& items) { |
| 288 | if (!qApp->feedUpdateLock()->tryLock()) { |
| 289 | // Lock was not obtained because |
| 290 | // it is used probably by feed updater or application |
| 291 | // is quitting. |
| 292 | qApp->showGuiMessage(Notification::Event::GeneralEvent, |
| 293 | {tr("Cannot edit item"), |
| 294 | tr("Selected item cannot be edited because another critical operation is ongoing."), |
| 295 | QSystemTrayIcon::MessageIcon::Warning}); |
| 296 | |
| 297 | // Thus, cannot delete and quit the method. |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | if (items.isEmpty()) { |
| 302 | qApp->feedUpdateLock()->unlock(); |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | auto std_editable_items = boolinq::from(items) |
| 307 | .where([](RootItem* it) { |
| 308 | return it->canBeEdited(); |
| 309 | }) |
| 310 | .distinct() |
| 311 | .toStdList(); |
| 312 | |
| 313 | if (std_editable_items.empty()) { |
| 314 | qApp->showGuiMessage(Notification::Event::GeneralEvent, |
| 315 | {tr("Cannot edit items"), |
| 316 | tr("Selected items cannot be edited. This is not supported (yet)."), |
| 317 | QSystemTrayIcon::MessageIcon::Critical}); |
| 318 | |
| 319 | qApp->feedUpdateLock()->unlock(); |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | if (std_editable_items.front()->kind() == RootItem::Kind::ServiceRoot && std_editable_items.size() > 1) { |
| 324 | qApp->showGuiMessage(Notification::Event::GeneralEvent, |
| 325 | {tr("Cannot edit items"), |
| 326 | tr("%1 does not support batch editing of multiple accounts.").arg(QSL(APP_NAME)), |
| 327 | QSystemTrayIcon::MessageIcon::Critical}); |
| 328 | |
| 329 | qApp->feedUpdateLock()->unlock(); |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | // We also check if items are from single account, if not we end. |
| 334 | std::list<ServiceRoot*> distinct_accounts = boolinq::from(std_editable_items) |
| 335 | .select([](RootItem* it) { |
| 336 | return it->getParentServiceRoot(); |
| 337 | }) |
| 338 | .distinct() |
| 339 | .toStdList(); |
| 340 | |
| 341 | if (distinct_accounts.size() != 1) { |
| 342 | qApp->showGuiMessage(Notification::Event::GeneralEvent, |
| 343 | {tr("Cannot edit items"), |
| 344 | tr("%1 does not support batch editing of items from multiple accounts.").arg(QSL(APP_NAME)), |
| 345 | QSystemTrayIcon::MessageIcon::Critical}); |
| 346 | |
| 347 | qApp->feedUpdateLock()->unlock(); |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | std::list<RootItem::Kind> distinct_types = boolinq::from(std_editable_items) |
| 352 | .select([](RootItem* it) { |
| 353 | return it->kind(); |
| 354 | }) |
| 355 | .distinct() |
| 356 | .toStdList(); |
| 357 | |
| 358 | if (distinct_types.size() != 1) { |
| 359 | qApp->showGuiMessage(Notification::Event::GeneralEvent, |
| 360 | {tr("Cannot edit items"), |
| 361 | tr("%1 does not support batch editing of items of varying types.").arg(QSL(APP_NAME)), |
| 362 | QSystemTrayIcon::MessageIcon::Critical}); |
| 363 | |
| 364 | qApp->feedUpdateLock()->unlock(); |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | if (qsizetype(std_editable_items.size()) < items.size()) { |
| 369 | // Some items are not editable. |
| 370 | qApp->showGuiMessage(Notification::Event::GeneralEvent, |
| 371 | {tr("Cannot edit some items"), |
| 372 | tr("Some of selected items cannot be edited. Proceeding to edit the rest."), |
| 373 | QSystemTrayIcon::MessageIcon::Warning}); |
| 374 | } |
| 375 | |
| 376 | distinct_accounts.front()->editItems(FROM_STD_LIST(QList<RootItem*>, std_editable_items)); |
| 377 | |
| 378 | // Changes are done, unlock the update master lock. |
| 379 | qApp->feedUpdateLock()->unlock(); |
| 380 | } |
| 381 | |
| 382 | void FeedsView::editChildFeeds() { |
| 383 | auto items = selectedFeeds(false); |
| 384 | |
| 385 | if (!items.isEmpty()) { |
| 386 | auto root_items = boolinq::from(items) |
| 387 | .select([](Feed* fd) { |
| 388 | return fd; |
| 389 | }) |
| 390 | .toStdList(); |
| 391 | |
| 392 | editItems(FROM_STD_LIST(QList<RootItem*>, root_items)); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | void FeedsView::editRecursiveFeeds() { |
| 397 | auto items = selectedFeeds(true); |
| 398 | |
| 399 | if (!items.isEmpty()) { |
| 400 | auto root_items = boolinq::from(items) |
| 401 | .select([](Feed* fd) { |
| 402 | return fd; |
| 403 | }) |
| 404 | .toStdList(); |
| 405 | |
| 406 | editItems(FROM_STD_LIST(QList<RootItem*>, root_items)); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | void FeedsView::changeFilter(FeedsProxyModel::FeedListFilter filter) { |
| 411 | m_proxyModel->setFeedListFilter(filter); |
| 412 | } |
| 413 | |
| 414 | void FeedsView::editSelectedItems() { |
| 415 | editItems(selectedItems()); |
| 416 | } |
| 417 | |
| 418 | void FeedsView::deleteSelectedItem() { |
| 419 | if (!qApp->feedUpdateLock()->tryLock()) { |
| 420 | // Lock was not obtained because |
| 421 | // it is used probably by feed updater or application |
| 422 | // is quitting. |
| 423 | qApp->showGuiMessage(Notification::Event::GeneralEvent, |
| 424 | {tr("Cannot delete item"), |
| 425 | tr("Selected item cannot be deleted because another critical operation is ongoing."), |
| 426 | QSystemTrayIcon::MessageIcon::Warning}); |
| 427 | |
| 428 | // Thus, cannot delete and quit the method. |
| 429 | return; |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | if (!currentIndex().isValid()) { |
| 434 | qApp->feedUpdateLock()->unlock(); |
| 435 | return; |
| 436 | } |
| 437 | */ |
| 438 | |
| 439 | QList<RootItem*> selected_items = selectedItems(); |
| 440 | auto std_deletable_items = boolinq::from(selected_items) |
| 441 | .where([](RootItem* it) { |
| 442 | return it->canBeDeleted(); |
| 443 | }) |
| 444 | .toStdList(); |
| 445 | |
| 446 | if (std_deletable_items.empty()) { |
| 447 | qApp->feedUpdateLock()->unlock(); |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | if (qsizetype(std_deletable_items.size()) < selected_items.size()) { |
| 452 | qApp->showGuiMessage(Notification::Event::GeneralEvent, |
| 453 | GuiMessage(tr("Some items won't be deleted"), |
| 454 | tr("Some selected items will not be deleted, because they cannot be deleted."), |
| 455 | QSystemTrayIcon::MessageIcon::Warning)); |
| 456 | } |
| 457 | |
| 458 | // Ask user first. |
| 459 | if (MsgBox::show(qApp->mainFormWidget(), |
| 460 | QMessageBox::Icon::Question, |
| 461 | tr("Deleting %n items", nullptr, int(std_deletable_items.size())), |
| 462 | tr("You are about to completely delete %n items.", nullptr, int(std_deletable_items.size())), |
| 463 | tr("Are you sure?"), |
| 464 | QString(), |
| 465 | QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No, |
| 466 | QMessageBox::StandardButton::Yes) == QMessageBox::StandardButton::No) { |
| 467 | // User refused. |
| 468 | qApp->feedUpdateLock()->unlock(); |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | auto std_pointed_items = boolinq::from(std_deletable_items) |
| 473 | .select([](RootItem* it) { |
| 474 | return QPointer<RootItem>(it); |
| 475 | }) |
| 476 | .toStdList(); |
| 477 | |
| 478 | for (const QPointer<RootItem>& pnt : std_pointed_items) { |
| 479 | if (pnt.isNull()) { |
| 480 | continue; |
| 481 | } |
| 482 | |
| 483 | if (pnt->deleteItem()) { |
| 484 | m_proxyModel->invalidate(); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // Changes are done, unlock the update master lock. |
| 489 | qApp->feedUpdateLock()->unlock(); |
| 490 | } |
| 491 | |
| 492 | void FeedsView::moveSelectedItemUp() { |
| 493 | auto its = selectedItems(); |
| 494 | auto std_its = boolinq::from(its) |
| 495 | .orderBy([](RootItem* it) { |
| 496 | return it->sortOrder(); |
| 497 | }) |
| 498 | .toStdList(); |
| 499 | |
| 500 | for (RootItem* it : std_its) { |
| 501 | m_sourceModel->changeSortOrder(it, false, false, it->sortOrder() - 1); |
| 502 | } |
| 503 | |
| 504 | m_proxyModel->invalidate(); |
| 505 | } |
| 506 | |
| 507 | void FeedsView::moveSelectedItemDown() { |
| 508 | auto its = selectedItems(); |
| 509 | auto std_its = boolinq::from(its) |
| 510 | .orderBy([](RootItem* it) { |
| 511 | return it->sortOrder(); |
| 512 | }) |
| 513 | .reverse() |
| 514 | .toStdList(); |
| 515 | |
| 516 | for (RootItem* it : std_its) { |
| 517 | m_sourceModel->changeSortOrder(it, false, false, it->sortOrder() + 1); |
| 518 | } |
| 519 | |
| 520 | m_proxyModel->invalidate(); |
| 521 | } |
| 522 | |
| 523 | void FeedsView::moveSelectedItemTop() { |
| 524 | for (RootItem* it : selectedItems()) { |
| 525 | m_sourceModel->changeSortOrder(it, true, false); |
| 526 | } |
| 527 | |
| 528 | m_proxyModel->invalidate(); |
| 529 | } |
| 530 | |
| 531 | void FeedsView::moveSelectedItemBottom() { |
| 532 | for (RootItem* it : selectedItems()) { |
| 533 | m_sourceModel->changeSortOrder(it, false, true); |
| 534 | } |
| 535 | |
| 536 | m_proxyModel->invalidate(); |
| 537 | } |
| 538 | |
| 539 | void FeedsView::rearrangeCategoriesOfSelectedItem() { |
| 540 | for (RootItem* it : selectedItems()) { |
| 541 | m_sourceModel->sortDirectDescendants(it, RootItem::Kind::Category); |
| 542 | } |
| 543 | |
| 544 | m_proxyModel->invalidate(); |
| 545 | } |
| 546 | |
| 547 | void FeedsView::rearrangeFeedsOfSelectedItem() { |
| 548 | for (RootItem* it : selectedItems()) { |
| 549 | m_sourceModel->sortDirectDescendants(it, RootItem::Kind::Feed); |
| 550 | } |
| 551 | |
| 552 | m_proxyModel->invalidate(); |
| 553 | } |
| 554 | |
| 555 | void FeedsView::markSelectedItemReadStatus(RootItem::ReadStatus read) { |
| 556 | for (RootItem* it : selectedItems()) { |
| 557 | m_sourceModel->markItemRead(it, read); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | void FeedsView::markSelectedItemRead() { |
| 562 | markSelectedItemReadStatus(RootItem::ReadStatus::Read); |
| 563 | } |
| 564 | |
| 565 | void FeedsView::markSelectedItemUnread() { |
| 566 | markSelectedItemReadStatus(RootItem::ReadStatus::Unread); |
| 567 | } |
| 568 | |
| 569 | void FeedsView::markAllItemsReadStatus(RootItem::ReadStatus read) { |
| 570 | m_sourceModel->markItemRead(m_sourceModel->rootItem(), read); |
| 571 | } |
| 572 | |
| 573 | void FeedsView::markAllItemsRead() { |
| 574 | markAllItemsReadStatus(RootItem::ReadStatus::Read); |
| 575 | } |
| 576 | |
| 577 | void FeedsView::selectNextItem() { |
| 578 | QModelIndex index_next = moveCursor(QAbstractItemView::CursorAction::MoveDown, Qt::KeyboardModifier::NoModifier); |
| 579 | |
| 580 | if (index_next.isValid()) { |
| 581 | setCurrentIndex(index_next); |
| 582 | scrollTo(index_next, QAbstractItemView::ScrollHint::EnsureVisible); |
| 583 | } |
| 584 | |
| 585 | setFocus(); |
| 586 | } |
| 587 | |
| 588 | void FeedsView::selectPreviousItem() { |
| 589 | QModelIndex index_previous = moveCursor(QAbstractItemView::CursorAction::MoveUp, Qt::KeyboardModifier::NoModifier); |
| 590 | |
| 591 | if (index_previous.isValid()) { |
| 592 | setCurrentIndex(index_previous); |
| 593 | scrollTo(index_previous, QAbstractItemView::ScrollHint::EnsureVisible); |
| 594 | } |
| 595 | |
| 596 | setFocus(); |
| 597 | } |
| 598 | |
| 599 | void FeedsView::selectNextUnreadItem() { |
| 600 | QModelIndex next_unread_row; |
| 601 | |
| 602 | if (currentIndex().isValid()) { |
| 603 | next_unread_row = nextPreviousUnreadItem(currentIndex()); |
| 604 | } |
| 605 | else { |
| 606 | next_unread_row = nextPreviousUnreadItem(m_proxyModel->index(0, MSG_DB_READ_INDEX)); |
| 607 | } |
| 608 | |
| 609 | if (next_unread_row.isValid()) { |
| 610 | setCurrentIndex(next_unread_row); |
| 611 | scrollTo(next_unread_row, QAbstractItemView::ScrollHint::EnsureVisible); |
| 612 | emit requestViewNextUnreadMessage(); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | QModelIndex FeedsView::nextPreviousUnreadItem(const QModelIndex& default_row) { |
| 617 | const bool started_from_zero = default_row.row() == 0 && !default_row.parent().isValid(); |
| 618 | QModelIndex next_index = nextUnreadItem(default_row); |
| 619 | |
| 620 | // There is no next message, check previous. |
| 621 | if (!next_index.isValid() && !started_from_zero) { |
| 622 | next_index = nextUnreadItem(m_proxyModel->index(0, 0)); |
| 623 | } |
| 624 | |
| 625 | return next_index; |
| 626 | } |
| 627 | |
| 628 | QModelIndex FeedsView::nextUnreadItem(const QModelIndex& default_row) { |
| 629 | QModelIndex nconst_default_row = m_proxyModel->index(default_row.row(), 0, default_row.parent()); |
| 630 | const QModelIndex starting_row = default_row; |
| 631 | |
| 632 | while (true) { |
| 633 | bool has_unread = |
| 634 | m_sourceModel->itemForIndex(m_proxyModel->mapToSource(nconst_default_row))->countOfUnreadMessages() > 0; |
| 635 | |
| 636 | if (has_unread) { |
| 637 | if (m_proxyModel->hasChildren(nconst_default_row)) { |
| 638 | // Current index has unread items, but is expandable, go to first child. |
| 639 | expand(nconst_default_row); |
| 640 | nconst_default_row = indexBelow(nconst_default_row); |
| 641 | continue; |
| 642 | } |
| 643 | else { |
| 644 | // We found unread feed, return it. |
| 645 | return nconst_default_row; |
| 646 | } |
| 647 | } |
| 648 | else { |
| 649 | QModelIndex next_row = indexBelow(nconst_default_row); |
| 650 | |
| 651 | if (next_row == nconst_default_row || !next_row.isValid() || starting_row == next_row) { |
| 652 | // We came to last row probably. |
| 653 | break; |
| 654 | } |
| 655 | else { |
| 656 | nconst_default_row = next_row; |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | return QModelIndex(); |
| 662 | } |
| 663 | |
| 664 | QMenu* FeedsView::initializeContextMenuBin(RootItem* clicked_item) { |
| 665 | if (m_contextMenuBin == nullptr) { |
| 666 | m_contextMenuBin = new QMenu(tr("Context menu for recycle bins"), this); |
| 667 | } |
| 668 | else { |
| 669 | m_contextMenuBin->clear(); |
| 670 | } |
| 671 | |
| 672 | QList<QAction*> specific_actions = clicked_item->contextMenuFeedsList(); |
| 673 | |
| 674 | m_contextMenuBin->addActions({qApp->mainForm()->m_ui->m_actionMarkSelectedItemsAsRead, |
| 675 | qApp->mainForm()->m_ui->m_actionMarkSelectedItemsAsUnread}); |
| 676 | |
| 677 | if (!specific_actions.isEmpty()) { |
| 678 | m_contextMenuBin->addSeparator(); |
| 679 | m_contextMenuBin->addActions(specific_actions); |
| 680 | } |
| 681 | |
| 682 | return m_contextMenuBin; |
| 683 | } |
| 684 | |
| 685 | QMenu* FeedsView::initializeContextMenuService(RootItem* clicked_item) { |
| 686 | if (m_contextMenuService == nullptr) { |
| 687 | m_contextMenuService = new QMenu(tr("Context menu for accounts"), this); |
| 688 | } |
| 689 | else { |
| 690 | m_contextMenuService->clear(); |
| 691 | } |
| 692 | |
| 693 | QList<QAction*> specific_actions = clicked_item->contextMenuFeedsList(); |
| 694 | |
| 695 | m_contextMenuService->addActions({qApp->mainForm()->m_ui->m_actionUpdateSelectedItems, |
| 696 | qApp->mainForm()->m_ui->m_actionEditSelectedItem, |
| 697 | qApp->mainForm()->m_ui->m_actionEditChildFeeds, |
| 698 | qApp->mainForm()->m_ui->m_actionEditChildFeedsRecursive, |
| 699 | qApp->mainForm()->m_ui->m_actionCopyUrlSelectedFeed, |
| 700 | qApp->mainForm()->m_ui->m_actionExpandCollapseItem, |
| 701 | qApp->mainForm()->m_ui->m_actionExpandCollapseItemRecursively, |
| 702 | qApp->mainForm()->m_ui->m_actionRearrangeCategories, |
| 703 | qApp->mainForm()->m_ui->m_actionRearrangeFeeds, |
| 704 | qApp->mainForm()->m_ui->m_actionMarkSelectedItemsAsRead, |
| 705 | qApp->mainForm()->m_ui->m_actionMarkSelectedItemsAsUnread, |
| 706 | qApp->mainForm()->m_ui->m_actionDeleteSelectedItem}); |
| 707 | |
| 708 | auto cat_add = clicked_item->getParentServiceRoot()->supportsCategoryAdding(); |
| 709 | auto feed_add = clicked_item->getParentServiceRoot()->supportsFeedAdding(); |
| 710 | |
| 711 | if (cat_add || feed_add) { |
| 712 | m_contextMenuService->addSeparator(); |
| 713 | } |
| 714 | |
| 715 | if (cat_add) { |
| 716 | m_contextMenuService->addAction(qApp->mainForm()->m_ui->m_actionAddCategoryIntoSelectedItem); |
| 717 | } |
| 718 | |
| 719 | if (feed_add) { |
| 720 | m_contextMenuService->addAction(qApp->mainForm()->m_ui->m_actionAddFeedIntoSelectedItem); |
| 721 | } |
| 722 | |
| 723 | if (!qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::SortAlphabetically)).toBool()) { |
| 724 | m_contextMenuService->addSeparator(); |
| 725 | m_contextMenuService->addAction(qApp->mainForm()->m_ui->m_actionFeedMoveUp); |
| 726 | m_contextMenuService->addAction(qApp->mainForm()->m_ui->m_actionFeedMoveDown); |
| 727 | m_contextMenuService->addAction(qApp->mainForm()->m_ui->m_actionFeedMoveTop); |
| 728 | m_contextMenuService->addAction(qApp->mainForm()->m_ui->m_actionFeedMoveBottom); |
| 729 | } |
| 730 | |
| 731 | if (!specific_actions.isEmpty()) { |
| 732 | m_contextMenuService->addSeparator(); |
| 733 | m_contextMenuService->addActions(specific_actions); |
| 734 | } |
| 735 | |
| 736 | return m_contextMenuService; |
| 737 | } |
| 738 | |
| 739 | void FeedsView::switchVisibility() { |
| 740 | setVisible(!isVisible()); |
| 741 | } |
| 742 | |
| 743 | void FeedsView::drawBranches(QPainter* painter, const QRect& rect, const QModelIndex& index) const { |
| 744 | if (!rootIsDecorated()) { |
| 745 | painter->save(); |
| 746 | painter->setOpacity(0.0); |
| 747 | } |
| 748 | |
| 749 | QTreeView::drawBranches(painter, rect, index); |
| 750 | |
| 751 | if (!rootIsDecorated()) { |
| 752 | painter->restore(); |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | void FeedsView::focusInEvent(QFocusEvent* event) { |
| 757 | QTreeView::focusInEvent(event); |
| 758 | |
| 759 | if (currentIndex().isValid()) { |
| 760 | selectionModel()->select(currentIndex(), |
| 761 | QItemSelectionModel::SelectionFlag::Select | QItemSelectionModel::SelectionFlag::Rows); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | void FeedsView::filterItems(SearchLineEdit::SearchMode mode, |
| 766 | Qt::CaseSensitivity sensitivity, |
| 767 | int custom_criteria, |
| 768 | const QString& phrase) { |
| 769 | if (!phrase.isEmpty()) { |
| 770 | m_dontSaveExpandState = true; |
| 771 | expandAll(); |
| 772 | m_dontSaveExpandState = false; |
| 773 | } |
| 774 | |
| 775 | qDebugNN << LOGSEC_GUI << "Running search of feeds with pattern" << QUOTE_W_SPACE_DOT(phrase); |
| 776 | |
| 777 | switch (mode) { |
| 778 | case SearchLineEdit::SearchMode::Wildcard: |
| 779 | m_proxyModel->setFilterWildcard(phrase); |
| 780 | break; |
| 781 | |
| 782 | case SearchLineEdit::SearchMode::RegularExpression: |
| 783 | m_proxyModel->setFilterRegularExpression(phrase); |
| 784 | break; |
| 785 | |
| 786 | case SearchLineEdit::SearchMode::FixedString: |
| 787 | default: |
| 788 | m_proxyModel->setFilterFixedString(phrase); |
| 789 | break; |
| 790 | } |
| 791 | |
| 792 | m_proxyModel->setFilterCaseSensitivity(sensitivity); |
| 793 | |
| 794 | BaseToolBar::SearchFields where_search = BaseToolBar::SearchFields(custom_criteria); |
| 795 | |
| 796 | m_proxyModel->setFilterKeyColumn(where_search == BaseToolBar::SearchFields::SearchTitleOnly ? FDS_MODEL_TITLE_INDEX |
| 797 | : -1); |
| 798 | |
| 799 | if (phrase.isEmpty()) { |
| 800 | loadAllExpandStates(); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | void FeedsView::toggleFeedSortingMode(bool sort_alphabetically) { |
| 805 | setSortingEnabled(sort_alphabetically); |
| 806 | m_proxyModel->setSortAlphabetically(sort_alphabetically); |
| 807 | } |
| 808 | |
| 809 | void FeedsView::onIndexExpanded(const QModelIndex& idx) { |
| 810 | qDebugNN << LOGSEC_GUI << "Feed list item expanded - " << m_proxyModel->data(idx).toString(); |
| 811 | |
| 812 | if (m_dontSaveExpandState) { |
| 813 | qWarningNN << LOGSEC_GUI << "Don't saving expand state - " << m_proxyModel->data(idx).toString(); |
| 814 | return; |
| 815 | } |
| 816 | |
| 817 | const RootItem* it = m_sourceModel->itemForIndex(m_proxyModel->mapToSource(idx)); |
| 818 | |
| 819 | if (it != nullptr && (int(it->kind()) & int(RootItem::Kind::Category | RootItem::Kind::ServiceRoot | |
| 820 | RootItem::Kind::Labels | RootItem::Kind::Probes)) > 0) { |
| 821 | const QString setting_name = it->hashCode(); |
| 822 | |
| 823 | qApp->settings()->setValue(GROUP(CategoriesExpandStates), setting_name, true); |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | void FeedsView::onIndexCollapsed(const QModelIndex& idx) { |
| 828 | qDebugNN << LOGSEC_GUI << "Feed list item collapsed - " << m_proxyModel->data(idx).toString(); |
| 829 | |
| 830 | if (m_dontSaveExpandState) { |
| 831 | qWarningNN << LOGSEC_GUI << "Don't saving collapse state - " << m_proxyModel->data(idx).toString(); |
| 832 | return; |
| 833 | } |
| 834 | |
| 835 | RootItem* it = m_sourceModel->itemForIndex(m_proxyModel->mapToSource(idx)); |
| 836 | |
| 837 | if (it != nullptr && (int(it->kind()) & int(RootItem::Kind::Category | RootItem::Kind::ServiceRoot | |
| 838 | RootItem::Kind::Labels | RootItem::Kind::Probes)) > 0) { |
| 839 | const QString setting_name = it->hashCode(); |
| 840 | |
| 841 | qApp->settings()->setValue(GROUP(CategoriesExpandStates), setting_name, false); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | void FeedsView::reloadDelayedExpansions() { |
| 846 | qDebugNN << LOGSEC_GUI << "Reloading delayed feed list expansions."; |
| 847 | |
| 848 | m_expansionDelayer.stop(); |
| 849 | m_dontSaveExpandState = true; |
| 850 | |
| 851 | for (const QPair<QModelIndex, bool>& exp : m_delayedItemExpansions) { |
| 852 | auto idx = m_proxyModel->mapFromSource(exp.first); |
| 853 | |
| 854 | if (idx.isValid()) { |
| 855 | setExpanded(idx, exp.second); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | m_dontSaveExpandState = false; |
| 860 | m_delayedItemExpansions.clear(); |
| 861 | } |
| 862 | |
| 863 | void FeedsView::onItemExpandStateSaveRequested(RootItem* item) { |
| 864 | saveExpandStates(item); |
| 865 | } |
| 866 | |
| 867 | void FeedsView::saveAllExpandStates() { |
| 868 | saveExpandStates(sourceModel()->rootItem()); |
| 869 | } |
| 870 | |
| 871 | void FeedsView::saveExpandStates(RootItem* item) { |
| 872 | Settings* settings = qApp->settings(); |
| 873 | QList<RootItem*> items = item->getSubTree(RootItem::Kind::Category | RootItem::Kind::ServiceRoot | |
| 874 | RootItem::Kind::Labels | RootItem::Kind::Probes); |
| 875 | |
| 876 | // Iterate all categories and save their expand statuses. |
| 877 | for (const RootItem* it : items) { |
| 878 | const QString setting_name = it->hashCode(); |
| 879 | QModelIndex source_index = sourceModel()->indexForItem(it); |
| 880 | QModelIndex visible_index = model()->mapFromSource(source_index); |
| 881 | |
| 882 | settings->setValue(GROUP(CategoriesExpandStates), setting_name, isExpanded(visible_index)); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | void FeedsView::loadAllExpandStates() { |
| 887 | const Settings* settings = qApp->settings(); |
| 888 | QList<RootItem*> expandable_items; |
| 889 | |
| 890 | expandable_items.append(sourceModel()->rootItem()->getSubTree(RootItem::Kind::Category | RootItem::Kind::ServiceRoot | |
| 891 | RootItem::Kind::Labels | RootItem::Kind::Probes)); |
| 892 | |
| 893 | // Iterate all categories and save their expand statuses. |
| 894 | for (const RootItem* item : expandable_items) { |
| 895 | const QString setting_name = item->hashCode(); |
| 896 | |
| 897 | setExpanded(model()->mapFromSource(sourceModel()->indexForItem(item)), |
| 898 | settings->value(GROUP(CategoriesExpandStates), setting_name, item->childCount() > 0).toBool()); |
| 899 | } |
| 900 | |
| 901 | sortByColumn(qApp->settings()->value(GROUP(GUI), SETTING(GUI::DefaultSortColumnFeeds)).toInt(), |
| 902 | static_cast<Qt::SortOrder>(qApp->settings() |
| 903 | ->value(GROUP(GUI), SETTING(GUI::DefaultSortOrderFeeds)) |
| 904 | .toInt())); |
| 905 | } |
| 906 | |
| 907 | void FeedsView::reloadItemExpandState(const QModelIndex& source_idx) { |
| 908 | // Model requests to expand some items as they are visible and there is |
| 909 | // a filter active, so they maybe were not visible before. |
| 910 | |
| 911 | RootItem* it = m_sourceModel->itemForIndex(source_idx); |
| 912 | |
| 913 | if (it == nullptr) { |
| 914 | return; |
| 915 | } |
| 916 | |
| 917 | const QString setting_name = it->hashCode(); |
| 918 | const bool expand = |
| 919 | qApp->settings()->value(GROUP(CategoriesExpandStates), setting_name, it->childCount() > 0).toBool(); |
| 920 | |
| 921 | m_delayedItemExpansions.append({source_idx, expand}); |
| 922 | m_expansionDelayer.start(300); |
| 923 | } |
| 924 | |
| 925 | QMenu* FeedsView::initializeContextMenuCategories(RootItem* clicked_item) { |
| 926 | if (m_contextMenuCategories == nullptr) { |
| 927 | m_contextMenuCategories = new QMenu(tr("Context menu for categories"), this); |
| 928 | } |
| 929 | else { |
| 930 | m_contextMenuCategories->clear(); |
| 931 | } |
| 932 | |
| 933 | QList<QAction*> specific_actions = clicked_item->contextMenuFeedsList(); |
| 934 | |
| 935 | m_contextMenuCategories->addActions({qApp->mainForm()->m_ui->m_actionUpdateSelectedItems, |
| 936 | qApp->mainForm()->m_ui->m_actionEditSelectedItem, |
| 937 | qApp->mainForm()->m_ui->m_actionEditChildFeeds, |
| 938 | qApp->mainForm()->m_ui->m_actionEditChildFeedsRecursive, |
| 939 | qApp->mainForm()->m_ui->m_actionCopyUrlSelectedFeed, |
| 940 | qApp->mainForm()->m_ui->m_actionExpandCollapseItem, |
| 941 | qApp->mainForm()->m_ui->m_actionExpandCollapseItemRecursively, |
| 942 | qApp->mainForm()->m_ui->m_actionRearrangeCategories, |
| 943 | qApp->mainForm()->m_ui->m_actionRearrangeFeeds, |
| 944 | qApp->mainForm()->m_ui->m_actionMarkSelectedItemsAsRead, |
| 945 | qApp->mainForm()->m_ui->m_actionMarkSelectedItemsAsUnread, |
| 946 | qApp->mainForm()->m_ui->m_actionDeleteSelectedItem}); |
| 947 | |
| 948 | auto cat_add = clicked_item->getParentServiceRoot()->supportsCategoryAdding(); |
| 949 | auto feed_add = clicked_item->getParentServiceRoot()->supportsFeedAdding(); |
| 950 | |
| 951 | if (cat_add || feed_add) { |
| 952 | m_contextMenuCategories->addSeparator(); |
| 953 | } |
| 954 | |
| 955 | if (cat_add) { |
| 956 | m_contextMenuCategories->addAction(qApp->mainForm()->m_ui->m_actionAddCategoryIntoSelectedItem); |
| 957 | } |
| 958 | |
| 959 | if (feed_add) { |
| 960 | m_contextMenuCategories->addAction(qApp->mainForm()->m_ui->m_actionAddFeedIntoSelectedItem); |
| 961 | } |
| 962 | |
| 963 | if (!qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::SortAlphabetically)).toBool()) { |
| 964 | m_contextMenuCategories->addSeparator(); |
| 965 | m_contextMenuCategories->addAction(qApp->mainForm()->m_ui->m_actionFeedMoveUp); |
| 966 | m_contextMenuCategories->addAction(qApp->mainForm()->m_ui->m_actionFeedMoveDown); |
| 967 | m_contextMenuCategories->addAction(qApp->mainForm()->m_ui->m_actionFeedMoveTop); |
| 968 | m_contextMenuCategories->addAction(qApp->mainForm()->m_ui->m_actionFeedMoveBottom); |
| 969 | } |
| 970 | |
| 971 | if (!specific_actions.isEmpty()) { |
| 972 | m_contextMenuCategories->addSeparator(); |
| 973 | m_contextMenuCategories->addActions(specific_actions); |
| 974 | } |
| 975 | |
| 976 | return m_contextMenuCategories; |
| 977 | } |
| 978 | |
| 979 | QMenu* FeedsView::initializeContextMenuFeeds(RootItem* clicked_item) { |
| 980 | if (m_contextMenuFeeds == nullptr) { |
| 981 | m_contextMenuFeeds = new QMenu(tr("Context menu for categories"), this); |
| 982 | } |
| 983 | else { |
| 984 | m_contextMenuFeeds->clear(); |
| 985 | } |
| 986 | |
| 987 | QList<QAction*> specific_actions = clicked_item->contextMenuFeedsList(); |
| 988 | |
| 989 | m_contextMenuFeeds->addActions(QList<QAction*>() << qApp->mainForm()->m_ui->m_actionUpdateSelectedItems |
| 990 | << qApp->mainForm()->m_ui->m_actionEditSelectedItem |
| 991 | << qApp->mainForm()->m_ui->m_actionCopyUrlSelectedFeed |
| 992 | << qApp->mainForm()->m_ui->m_actionMarkSelectedItemsAsRead |
| 993 | << qApp->mainForm()->m_ui->m_actionMarkSelectedItemsAsUnread |
| 994 | << qApp->mainForm()->m_ui->m_actionPurgeSelectedItems |
| 995 | << qApp->mainForm()->m_ui->m_actionDeleteSelectedItem); |
| 996 | |
| 997 | auto cat_add = clicked_item->getParentServiceRoot()->supportsCategoryAdding(); |
| 998 | auto feed_add = clicked_item->getParentServiceRoot()->supportsFeedAdding(); |
| 999 | |
| 1000 | if (cat_add || feed_add) { |
| 1001 | m_contextMenuFeeds->addSeparator(); |
| 1002 | } |
| 1003 | |
| 1004 | if (cat_add) { |
| 1005 | m_contextMenuFeeds->addAction(qApp->mainForm()->m_ui->m_actionAddCategoryIntoSelectedItem); |
| 1006 | } |
| 1007 | |
| 1008 | if (feed_add) { |
| 1009 | m_contextMenuFeeds->addAction(qApp->mainForm()->m_ui->m_actionAddFeedIntoSelectedItem); |
| 1010 | } |
| 1011 | |
| 1012 | if (!qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::SortAlphabetically)).toBool()) { |
| 1013 | m_contextMenuFeeds->addSeparator(); |
| 1014 | m_contextMenuFeeds->addAction(qApp->mainForm()->m_ui->m_actionFeedMoveUp); |
| 1015 | m_contextMenuFeeds->addAction(qApp->mainForm()->m_ui->m_actionFeedMoveDown); |
| 1016 | m_contextMenuFeeds->addAction(qApp->mainForm()->m_ui->m_actionFeedMoveTop); |
| 1017 | m_contextMenuFeeds->addAction(qApp->mainForm()->m_ui->m_actionFeedMoveBottom); |
| 1018 | } |
| 1019 | |
| 1020 | if (!specific_actions.isEmpty()) { |
| 1021 | m_contextMenuFeeds->addSeparator(); |
| 1022 | m_contextMenuFeeds->addActions(specific_actions); |
| 1023 | } |
| 1024 | |
| 1025 | return m_contextMenuFeeds; |
| 1026 | } |
| 1027 | |
| 1028 | QMenu* FeedsView::initializeContextMenuImportant(RootItem* clicked_item) { |
| 1029 | if (m_contextMenuImportant == nullptr) { |
| 1030 | m_contextMenuImportant = new QMenu(tr("Context menu for important articles"), this); |
| 1031 | } |
| 1032 | else { |
| 1033 | m_contextMenuImportant->clear(); |
| 1034 | } |
| 1035 | |
| 1036 | QList<QAction*> specific_actions = clicked_item->contextMenuFeedsList(); |
| 1037 | |
| 1038 | m_contextMenuImportant->addActions({qApp->mainForm()->m_ui->m_actionMarkSelectedItemsAsRead, |
| 1039 | qApp->mainForm()->m_ui->m_actionMarkSelectedItemsAsUnread}); |
| 1040 | |
| 1041 | if (!specific_actions.isEmpty()) { |
| 1042 | m_contextMenuImportant->addSeparator(); |
| 1043 | m_contextMenuImportant->addActions(specific_actions); |
| 1044 | } |
| 1045 | |
| 1046 | return m_contextMenuImportant; |
| 1047 | } |
| 1048 | |
| 1049 | QMenu* FeedsView::initializeContextMenuEmptySpace() { |
| 1050 | if (m_contextMenuEmptySpace == nullptr) { |
| 1051 | m_contextMenuEmptySpace = new QMenu(tr("Context menu for empty space"), this); |
| 1052 | m_contextMenuEmptySpace->addMenu(qApp->mainForm()->m_ui->m_menuAddItem); |
| 1053 | m_contextMenuEmptySpace->addSeparator(); |
| 1054 | } |
| 1055 | |
| 1056 | return m_contextMenuEmptySpace; |
| 1057 | } |
| 1058 | |
| 1059 | QMenu* FeedsView::initializeContextMenuOtherItem(RootItem* clicked_item) { |
| 1060 | if (m_contextMenuOtherItems == nullptr) { |
| 1061 | m_contextMenuOtherItems = new QMenu(tr("Context menu for other items"), this); |
| 1062 | } |
| 1063 | else { |
| 1064 | m_contextMenuOtherItems->clear(); |
| 1065 | } |
| 1066 | |
| 1067 | QList<QAction*> specific_actions = clicked_item->contextMenuFeedsList(); |
| 1068 | |
| 1069 | if (!specific_actions.isEmpty()) { |
| 1070 | m_contextMenuOtherItems->addSeparator(); |
| 1071 | m_contextMenuOtherItems->addActions(specific_actions); |
| 1072 | } |
| 1073 | else { |
| 1074 | m_contextMenuOtherItems->addAction(qApp->mainForm()->m_ui->m_actionNoActions); |
| 1075 | } |
| 1076 | |
| 1077 | return m_contextMenuOtherItems; |
| 1078 | } |
| 1079 | |
| 1080 | QMenu* FeedsView::initializeContextMenuLabel(RootItem* clicked_item) { |
| 1081 | if (m_contextMenuLabel == nullptr) { |
| 1082 | m_contextMenuLabel = new QMenu(tr("Context menu for label"), this); |
| 1083 | } |
| 1084 | else { |
| 1085 | m_contextMenuLabel->clear(); |
| 1086 | } |
| 1087 | |
| 1088 | QList<QAction*> specific_actions = clicked_item->contextMenuFeedsList(); |
| 1089 | |
| 1090 | m_contextMenuLabel->addAction(qApp->mainForm()->m_ui->m_actionEditSelectedItem); |
| 1091 | m_contextMenuLabel->addAction(qApp->mainForm()->m_ui->m_actionMarkSelectedItemsAsRead); |
| 1092 | m_contextMenuLabel->addAction(qApp->mainForm()->m_ui->m_actionMarkSelectedItemsAsUnread); |
| 1093 | m_contextMenuLabel->addAction(qApp->mainForm()->m_ui->m_actionDeleteSelectedItem); |
| 1094 | |
| 1095 | if (!specific_actions.isEmpty()) { |
| 1096 | m_contextMenuLabel->addSeparator(); |
| 1097 | m_contextMenuLabel->addActions(specific_actions); |
| 1098 | } |
| 1099 | |
| 1100 | return m_contextMenuLabel; |
| 1101 | } |
| 1102 | |
| 1103 | QMenu* FeedsView::initializeContextMenuProbe(RootItem* clicked_item) { |
| 1104 | if (m_contextMenuProbe == nullptr) { |
| 1105 | m_contextMenuProbe = new QMenu(tr("Context menu for regex query"), this); |
| 1106 | } |
| 1107 | else { |
| 1108 | m_contextMenuProbe->clear(); |
| 1109 | } |
| 1110 | |
| 1111 | QList<QAction*> specific_actions = clicked_item->contextMenuFeedsList(); |
| 1112 | |
| 1113 | m_contextMenuProbe->addAction(qApp->mainForm()->m_ui->m_actionEditSelectedItem); |
| 1114 | m_contextMenuProbe->addAction(qApp->mainForm()->m_ui->m_actionMarkSelectedItemsAsRead); |
| 1115 | m_contextMenuProbe->addAction(qApp->mainForm()->m_ui->m_actionMarkSelectedItemsAsUnread); |
| 1116 | m_contextMenuProbe->addAction(qApp->mainForm()->m_ui->m_actionDeleteSelectedItem); |
| 1117 | |
| 1118 | if (!specific_actions.isEmpty()) { |
| 1119 | m_contextMenuProbe->addSeparator(); |
| 1120 | m_contextMenuProbe->addActions(specific_actions); |
| 1121 | } |
| 1122 | |
| 1123 | return m_contextMenuProbe; |
| 1124 | } |
| 1125 | |
| 1126 | QByteArray FeedsView::saveHeaderState() const { |
| 1127 | QJsonObject obj; |
| 1128 | |
| 1129 | obj[QSL("header_count")] = header()->count(); |
| 1130 | |
| 1131 | // Store column attributes. |
| 1132 | for (int i = 0; i < header()->count(); i++) { |
| 1133 | obj[QSL("header_%1_size").arg(i)] = header()->sectionSize(i); |
| 1134 | } |
| 1135 | |
| 1136 | return QJsonDocument(obj).toJson(QJsonDocument::JsonFormat::Compact); |
| 1137 | } |
| 1138 | |
| 1139 | void FeedsView::restoreHeaderState(const QByteArray& dta) { |
| 1140 | QJsonObject obj = QJsonDocument::fromJson(dta).object(); |
| 1141 | int saved_header_count = obj[QSL("header_count")].toInt(); |
| 1142 | |
| 1143 | if (saved_header_count < header()->count()) { |
| 1144 | qWarningNN << LOGSEC_GUI << "Detected invalid state for feed list."; |
| 1145 | return; |
| 1146 | } |
| 1147 | |
| 1148 | // Restore column attributes. |
| 1149 | for (int i = 0; i < saved_header_count && i < header()->count(); i++) { |
| 1150 | int ss = obj[QSL("header_%1_size").arg(i)].toInt(); |
| 1151 | |
| 1152 | header()->resizeSection(i, ss); |
| 1153 | } |
| 1154 | |
| 1155 | // All columns are resizeable but last one is set to auto-stretch to fill remaining |
| 1156 | // space. Sometimes this column is saved as too wide and causes |
| 1157 | // horizontal scrollbar to appear. Therefore downsize it. |
| 1158 | header()->resizeSection(header()->logicalIndex(header()->count() - 1), 1); |
| 1159 | } |
| 1160 | |
| 1161 | void FeedsView::setupAppearance() { |
| 1162 | // Setup column resize strategies. |
| 1163 | for (int i = 0; i < header()->count(); i++) { |
| 1164 | header()->setSectionResizeMode(i, QHeaderView::ResizeMode::Interactive); |
| 1165 | } |
| 1166 | |
| 1167 | header()->setStretchLastSection(true); |
| 1168 | header()->setSectionsMovable(false); |
| 1169 | |
| 1170 | setUniformRowHeights(true); |
| 1171 | setAnimated(true); |
| 1172 | setSortingEnabled(false); |
| 1173 | setItemsExpandable(true); |
| 1174 | setAutoExpandDelay(800); |
| 1175 | setExpandsOnDoubleClick(true); |
| 1176 | setEditTriggers(QAbstractItemView::EditTrigger::NoEditTriggers); |
| 1177 | setIndentation(FEEDS_VIEW_INDENTATION); |
| 1178 | setAcceptDrops(true); |
| 1179 | viewport()->setAcceptDrops(true); |
| 1180 | setDragEnabled(true); |
| 1181 | setDropIndicatorShown(true); |
| 1182 | setDragDropMode(QAbstractItemView::DragDropMode::InternalMove); |
| 1183 | setAllColumnsShowFocus(false); |
| 1184 | setRootIsDecorated(false); |
| 1185 | setSelectionMode(QAbstractItemView::SelectionMode::ExtendedSelection); |
| 1186 | setItemDelegate(new StyledItemDelegateWithoutFocus(qApp->settings() |
| 1187 | ->value(GROUP(GUI), SETTING(GUI::HeightRowFeeds)) |
| 1188 | .toInt(), |
| 1189 | -1, |
| 1190 | this)); |
| 1191 | } |
| 1192 | |
| 1193 | void FeedsView::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) { |
| 1194 | RootItem* selected_item = selectedItem(); |
| 1195 | |
| 1196 | m_proxyModel->setSelectedItem(selected_item); |
| 1197 | m_sourceModel->reloadChangedLayout(m_proxyModel->mapSelectionToSource(deselected).indexes()); |
| 1198 | |
| 1199 | QTreeView::selectionChanged(selected, deselected); |
| 1200 | emit itemSelected(selected_item); |
| 1201 | |
| 1202 | if (!selectedIndexes().isEmpty() && |
| 1203 | qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::AutoExpandOnSelection)).toBool()) { |
| 1204 | expand(selectedIndexes().constFirst()); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | void FeedsView::keyPressEvent(QKeyEvent* event) { |
| 1209 | BaseTreeView::keyPressEvent(event); |
| 1210 | |
| 1211 | if (event->key() == Qt::Key::Key_Delete) { |
| 1212 | deleteSelectedItem(); |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | void FeedsView::contextMenuEvent(QContextMenuEvent* event) { |
| 1217 | const QModelIndex clicked_index = indexAt(event->pos()); |
| 1218 | |
| 1219 | if (clicked_index.isValid()) { |
| 1220 | const QModelIndex mapped_index = model()->mapToSource(clicked_index); |
| 1221 | RootItem* clicked_item = sourceModel()->itemForIndex(mapped_index); |
| 1222 | |
| 1223 | if (clicked_item->kind() == RootItem::Kind::Category) { |
| 1224 | // Display context menu for categories. |
| 1225 | initializeContextMenuCategories(clicked_item)->exec(event->globalPos()); |
| 1226 | } |
| 1227 | else if (clicked_item->kind() == RootItem::Kind::Feed) { |
| 1228 | // Display context menu for feeds. |
| 1229 | initializeContextMenuFeeds(clicked_item)->exec(event->globalPos()); |
| 1230 | } |
| 1231 | else if (clicked_item->kind() == RootItem::Kind::Important || clicked_item->kind() == RootItem::Kind::Unread) { |
| 1232 | initializeContextMenuImportant(clicked_item)->exec(event->globalPos()); |
| 1233 | } |
| 1234 | else if (clicked_item->kind() == RootItem::Kind::Bin) { |
| 1235 | initializeContextMenuBin(clicked_item)->exec(event->globalPos()); |
| 1236 | } |
| 1237 | else if (clicked_item->kind() == RootItem::Kind::ServiceRoot) { |
| 1238 | initializeContextMenuService(clicked_item)->exec(event->globalPos()); |
| 1239 | } |
| 1240 | else if (clicked_item->kind() == RootItem::Kind::Label) { |
| 1241 | initializeContextMenuLabel(clicked_item)->exec(event->globalPos()); |
| 1242 | } |
| 1243 | else if (clicked_item->kind() == RootItem::Kind::Probe) { |
| 1244 | initializeContextMenuProbe(clicked_item)->exec(event->globalPos()); |
| 1245 | } |
| 1246 | else { |
| 1247 | initializeContextMenuOtherItem(clicked_item)->exec(event->globalPos()); |
| 1248 | } |
| 1249 | } |
| 1250 | else { |
| 1251 | // Display menu for empty space. |
| 1252 | initializeContextMenuEmptySpace()->exec(event->globalPos()); |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | void FeedsView::mouseDoubleClickEvent(QMouseEvent* event) { |
| 1257 | QTreeView::mouseDoubleClickEvent(event); |
| 1258 | } |
| 1259 | |
| 1260 | void FeedsView::saveSortState(int column, Qt::SortOrder order) { |
| 1261 | qApp->settings()->setValue(GROUP(GUI), GUI::DefaultSortColumnFeeds, column); |
| 1262 | qApp->settings()->setValue(GROUP(GUI), GUI::DefaultSortOrderFeeds, order); |
| 1263 | } |
| 1264 | |
| 1265 | void FeedsView::validateItemAfterDragDrop(const QModelIndex& source_index) { |
| 1266 | const QModelIndex mapped = m_proxyModel->mapFromSource(source_index); |
| 1267 | |
| 1268 | if (mapped.isValid()) { |
| 1269 | expand(mapped); |
| 1270 | setCurrentIndex(mapped); |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | void FeedsView::onItemExpandRequested(const QList<RootItem*>& items, bool exp) { |
| 1275 | for (const RootItem* item : items) { |
| 1276 | QModelIndex source_index = m_sourceModel->indexForItem(item); |
| 1277 | QModelIndex proxy_index = m_proxyModel->mapFromSource(source_index); |
| 1278 | |
| 1279 | setExpanded(proxy_index, exp); |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | void FeedsView::drawRow(QPainter* painter, const QStyleOptionViewItem& options, const QModelIndex& index) const { |
| 1284 | auto opts = options; |
| 1285 | |
| 1286 | opts.decorationAlignment = Qt::AlignmentFlag::AlignLeft | Qt::AlignmentFlag::AlignVCenter; |
| 1287 | |
| 1288 | BaseTreeView::drawRow(painter, opts, index); |
| 1289 | } |