| 1 |
/* |
| 2 |
* This file is part of the apvlv package |
| 3 |
* |
| 4 |
* Copyright (C) 2008 Alf. |
| 5 |
* |
| 6 |
* Contact: Alf <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU General Public License |
| 10 |
* as published by the Free Software Foundation; either version 2.0 of |
| 11 |
* the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public |
| 19 |
* License along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
* |
| 22 |
*/ |
| 23 |
/* @CPPFILE ApvlvFrame.cc |
| 24 |
* |
| 25 |
* Author: Alf <[email protected]> |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <QApplication> |
| 29 |
#include <QFile> |
| 30 |
#include <QHeaderView> |
| 31 |
#include <QInputDialog> |
| 32 |
#include <QLocale> |
| 33 |
#include <QMessageBox> |
| 34 |
#include <QTimeZone> |
| 35 |
#include <stack> |
| 36 |
|
| 37 |
#include "ApvlvDirectory.h" |
| 38 |
#include "ApvlvFrame.h" |
| 39 |
#include "ApvlvParams.h" |
| 40 |
#include "ApvlvUtil.h" |
| 41 |
|
| 42 |
namespace apvlv |
| 43 |
{ |
| 44 |
using namespace std; |
| 45 |
|
| 46 |
std::vector<const char *> Directory::ColumnString = { |
| 47 |
QT_TR_NOOP ("Title"), |
| 48 |
QT_TR_NOOP ("Modified Time"), |
| 49 |
QT_TR_NOOP ("File Size"), |
| 50 |
}; |
| 51 |
std::vector<const char *> Directory::SortByColumnString = { |
| 52 |
QT_TR_NOOP ("Sort By Title"), |
| 53 |
QT_TR_NOOP ("Sort By Modified Time"), |
| 54 |
QT_TR_NOOP ("Sort By File Size"), |
| 55 |
}; |
| 56 |
std::vector<const char *> Directory::FilterTypeString = { |
| 57 |
QT_TR_NOOP ("Filter Title"), |
| 58 |
QT_TR_NOOP ("Filter File Name"), |
| 59 |
QT_TR_NOOP ("Filter Modified Time >="), |
| 60 |
QT_TR_NOOP ("Filter Modified Time <="), |
| 61 |
QT_TR_NOOP ("Filter File Size >="), |
| 62 |
QT_TR_NOOP ("Filter FileSize <="), |
| 63 |
}; |
| 64 |
|
| 65 |
void |
| 66 |
ContentTree::keyPressEvent (QKeyEvent *event) |
| 67 |
{ |
| 68 |
event->ignore (); |
| 69 |
} |
| 70 |
|
| 71 |
Directory::Directory () |
| 72 |
{ |
| 73 |
setLayout (&mLayout); |
| 74 |
mLayout.addWidget (&mToolBar, 0); |
| 75 |
mLayout.addWidget (&mTreeWidget); |
| 76 |
setupToolBar (); |
| 77 |
setupTree (); |
| 78 |
auto guioptions |
| 79 |
= ApvlvParams::instance ()->getStringOrDefault ("guioptions"); |
| 80 |
if (guioptions.find ('S') == string::npos) |
| 81 |
{ |
| 82 |
mToolBar.hide (); |
| 83 |
} |
| 84 |
QTimer::singleShot (50, this, SLOT (selectFirstItem ())); |
| 85 |
} |
| 86 |
|
| 87 |
void |
| 88 |
Directory::setupToolBar () |
| 89 |
{ |
| 90 |
mToolBar.addWidget (&mFilterText); |
| 91 |
QObject::connect (&mFilterText, SIGNAL (textEdited (const QString &)), this, |
| 92 |
SLOT (onFilter ())); |
| 93 |
mToolBar.addSeparator (); |
| 94 |
mToolBar.addWidget (&mFilterType); |
| 95 |
for (auto const &str : FilterTypeString) |
| 96 |
{ |
| 97 |
mFilterType.addItem (tr (str)); |
| 98 |
} |
| 99 |
mToolBar.addSeparator (); |
| 100 |
QObject::connect (&mFilterType, SIGNAL (activated (int)), this, |
| 101 |
SLOT (onFilter ())); |
| 102 |
|
| 103 |
auto refresh = mToolBar.addAction (tr ("Refresh")); |
| 104 |
refresh->setIcon (QIcon::fromTheme (QIcon::ThemeIcon::ViewRefresh)); |
| 105 |
QObject::connect (refresh, SIGNAL (triggered (bool)), this, |
| 106 |
SLOT (onRefresh ())); |
| 107 |
mToolBar.addSeparator (); |
| 108 |
|
| 109 |
auto expand_all = mToolBar.addAction (tr ("Expand All")); |
| 110 |
expand_all->setIcon (QIcon::fromTheme (QIcon::ThemeIcon::ListAdd)); |
| 111 |
QObject::connect (expand_all, SIGNAL (triggered (bool)), &mTreeWidget, |
| 112 |
SLOT (expandAll ())); |
| 113 |
auto collapse_all = mToolBar.addAction (tr ("Collapse All")); |
| 114 |
collapse_all->setIcon (QIcon::fromTheme (QIcon::ThemeIcon::ListRemove)); |
| 115 |
QObject::connect (collapse_all, SIGNAL (triggered (bool)), &mTreeWidget, |
| 116 |
SLOT (collapseAll ())); |
| 117 |
mToolBar.addSeparator (); |
| 118 |
|
| 119 |
mToolBar.addWidget (&mSortType); |
| 120 |
for (auto const &str : SortByColumnString) |
| 121 |
{ |
| 122 |
mSortType.addItem (tr (str)); |
| 123 |
} |
| 124 |
QObject::connect (&mSortType, SIGNAL (activated (int)), this, |
| 125 |
SLOT (sortBy (int))); |
| 126 |
} |
| 127 |
|
| 128 |
void |
| 129 |
Directory::setupTree () |
| 130 |
{ |
| 131 |
mTreeWidget.setColumnCount (3); |
| 132 |
mTreeWidget.setColumnWidth (static_cast<int> (Column::Title), 400); |
| 133 |
mTreeWidget.setColumnWidth (static_cast<int> (Column::MTime), 150); |
| 134 |
mTreeWidget.setColumnWidth (static_cast<int> (Column::FileSize), 150); |
| 135 |
mTreeWidget.setSortingEnabled (false); |
| 136 |
mTreeWidget.setHeaderHidden (false); |
| 137 |
QStringList labels; |
| 138 |
for (auto const &str : ColumnString) |
| 139 |
{ |
| 140 |
labels.append (tr (str)); |
| 141 |
} |
| 142 |
mTreeWidget.setHeaderLabels (labels); |
| 143 |
|
| 144 |
auto headerview = mTreeWidget.header (); |
| 145 |
headerview->setSectionsClickable (true); |
| 146 |
QObject::connect (headerview, SIGNAL (sectionClicked (int)), this, |
| 147 |
SLOT (sortBy (int))); |
| 148 |
|
| 149 |
mTreeWidget.setVerticalScrollMode ( |
| 150 |
QAbstractItemView::ScrollMode::ScrollPerItem); |
| 151 |
mTreeWidget.setSelectionBehavior ( |
| 152 |
QAbstractItemView::SelectionBehavior::SelectRows); |
| 153 |
mTreeWidget.setSelectionMode ( |
| 154 |
QAbstractItemView::SelectionMode::ExtendedSelection); |
| 155 |
|
| 156 |
mTypeIcons[FileIndexType::DIR] = QIcon (IconDir.c_str ()); |
| 157 |
mTypeIcons[FileIndexType::FILE] = QIcon (IconFile.c_str ()); |
| 158 |
mTypeIcons[FileIndexType::PAGE] = QIcon (IconPage.c_str ()); |
| 159 |
|
| 160 |
QObject::connect (&mTreeWidget, |
| 161 |
SIGNAL (itemActivated (QTreeWidgetItem *, int)), this, |
| 162 |
SLOT (onRowActivated (QTreeWidgetItem *, int))); |
| 163 |
QObject::connect (&mTreeWidget, |
| 164 |
SIGNAL (itemDoubleClicked (QTreeWidgetItem *, int)), this, |
| 165 |
SLOT (onRowDoubleClicked ())); |
| 166 |
mTreeWidget.setContextMenuPolicy (Qt::ContextMenuPolicy::CustomContextMenu); |
| 167 |
QObject::connect (&mTreeWidget, |
| 168 |
SIGNAL (customContextMenuRequested (const QPoint &)), this, |
| 169 |
SLOT (onContextMenuRequest (const QPoint &))); |
| 170 |
} |
| 171 |
|
| 172 |
bool |
| 173 |
Directory::isReady () |
| 174 |
{ |
| 175 |
return (mTreeWidget.topLevelItemCount () > 0); |
| 176 |
} |
| 177 |
|
| 178 |
void |
| 179 |
Directory::setIndex (const FileIndex &index) |
| 180 |
{ |
| 181 |
using enum FileIndexType; |
| 182 |
if (mTreeWidget.topLevelItemCount () == 0 || index.type == DIR) |
| 183 |
{ |
| 184 |
refreshIndex (index); |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
auto cur_index = currentItemFileIndex (); |
| 189 |
if (mIndex.type == DIR && cur_index->type == FILE && index.type == FILE) |
| 190 |
{ |
| 191 |
if (cur_index->mChildrenIndex.empty ()) |
| 192 |
{ |
| 193 |
auto cur_item = selectedTreeItem (); |
| 194 |
cur_index->moveChildChildren (index); |
| 195 |
for (auto &child : cur_index->mChildrenIndex) |
| 196 |
{ |
| 197 |
setIndex (child, cur_item); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
void |
| 204 |
Directory::setItemSelected (QTreeWidgetItem *item) |
| 205 |
{ |
| 206 |
auto selitems = mTreeWidget.selectedItems (); |
| 207 |
for (auto selitem : selitems) |
| 208 |
{ |
| 209 |
selitem->setSelected (false); |
| 210 |
} |
| 211 |
|
| 212 |
auto parent = item->parent (); |
| 213 |
while (parent) |
| 214 |
{ |
| 215 |
mTreeWidget.expandItem (parent); |
| 216 |
parent = parent->parent (); |
| 217 |
} |
| 218 |
|
| 219 |
item->setSelected (true); |
| 220 |
if (item->isExpanded ()) |
| 221 |
mTreeWidget.collapseItem (item); |
| 222 |
mTreeWidget.scrollToItem (item); |
| 223 |
} |
| 224 |
|
| 225 |
void |
| 226 |
Directory::setIndex (FileIndex &index, QTreeWidgetItem *root_itr) |
| 227 |
{ |
| 228 |
auto itr = new QTreeWidgetItem (); |
| 229 |
setFileIndexToTreeItem (itr, &index); |
| 230 |
|
| 231 |
root_itr->addChild (itr); |
| 232 |
|
| 233 |
for (auto &child : index.mChildrenIndex) |
| 234 |
{ |
| 235 |
setIndex (child, itr); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
void |
| 240 |
Directory::refreshIndex (const FileIndex &index) |
| 241 |
{ |
| 242 |
mTreeWidget.clear (); |
| 243 |
|
| 244 |
mIndex = index; |
| 245 |
|
| 246 |
for (auto &child : mIndex.mChildrenIndex) |
| 247 |
{ |
| 248 |
setIndex (child, mTreeWidget.invisibleRootItem ()); |
| 249 |
} |
| 250 |
|
| 251 |
sortItems (mTreeWidget.invisibleRootItem ()); |
| 252 |
|
| 253 |
QTimer::singleShot (50, this, SLOT (selectFirstItem ())); |
| 254 |
} |
| 255 |
|
| 256 |
void |
| 257 |
Directory::setFileIndexToTreeItem (QTreeWidgetItem *item, FileIndex *index) |
| 258 |
{ |
| 259 |
using enum Column; |
| 260 |
auto variant = QVariant::fromValue<FileIndex *> (index); |
| 261 |
item->setData (static_cast<int> (Title), Qt::UserRole, variant); |
| 262 |
item->setText (static_cast<int> (Title), |
| 263 |
QString::fromLocal8Bit (index->title.c_str ())); |
| 264 |
item->setIcon (static_cast<int> (Title), mTypeIcons[index->type]); |
| 265 |
item->setToolTip (static_cast<int> (Title), |
| 266 |
QString::fromLocal8Bit (index->path)); |
| 267 |
|
| 268 |
using enum FileIndexType; |
| 269 |
if (index->type == FILE) |
| 270 |
{ |
| 271 |
auto date = QDateTime::fromSecsSinceEpoch (index->mtime, |
| 272 |
QTimeZone::systemTimeZone ()); |
| 273 |
item->setText (static_cast<int> (Column::MTime), |
| 274 |
date.toString ("yyyy-MM-dd HH:mm:ss")); |
| 275 |
auto size |
| 276 |
= QLocale ().formattedDataSize (static_cast<qint64> (index->size)); |
| 277 |
item->setText (static_cast<int> (Column::FileSize), size); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
FileIndex * |
| 282 |
Directory::getFileIndexFromTreeItem (QTreeWidgetItem *item) |
| 283 |
{ |
| 284 |
if (item == nullptr) |
| 285 |
return nullptr; |
| 286 |
|
| 287 |
auto varaint = item->data (static_cast<int> (Column::Title), Qt::UserRole); |
| 288 |
auto index = varaint.value<FileIndex *> (); |
| 289 |
return index; |
| 290 |
} |
| 291 |
|
| 292 |
FileIndex * |
| 293 |
Directory::treeItemToFileIndex (QTreeWidgetItem *item) |
| 294 |
{ |
| 295 |
while (item != nullptr) |
| 296 |
{ |
| 297 |
auto index = getFileIndexFromTreeItem (item); |
| 298 |
if (index->type == FileIndexType::FILE) |
| 299 |
return index; |
| 300 |
else |
| 301 |
item = item->parent (); |
| 302 |
} |
| 303 |
|
| 304 |
return nullptr; |
| 305 |
} |
| 306 |
|
| 307 |
void |
| 308 |
Directory::filterItemBy (QTreeWidgetItem *root, const filterFunc &filter_func) |
| 309 |
{ |
| 310 |
std::stack<QTreeWidgetItem *> item_stack; |
| 311 |
for (auto i = 0; i < root->childCount (); ++i) |
| 312 |
{ |
| 313 |
auto item = root->child (i); |
| 314 |
item_stack.push (item); |
| 315 |
} |
| 316 |
|
| 317 |
while (!item_stack.empty ()) |
| 318 |
{ |
| 319 |
auto item = item_stack.top (); |
| 320 |
item_stack.pop (); |
| 321 |
|
| 322 |
auto index = getFileIndexFromTreeItem (item); |
| 323 |
auto [is_filter, same_as_file] = filter_func (index); |
| 324 |
if (is_filter) |
| 325 |
{ |
| 326 |
item->setHidden (false); |
| 327 |
|
| 328 |
auto parent = item->parent (); |
| 329 |
while (parent) |
| 330 |
{ |
| 331 |
parent->setHidden (false); |
| 332 |
parent = parent->parent (); |
| 333 |
} |
| 334 |
} |
| 335 |
else |
| 336 |
{ |
| 337 |
item->setHidden (true); |
| 338 |
} |
| 339 |
|
| 340 |
if (index->type == FileIndexType::FILE && same_as_file) |
| 341 |
{ |
| 342 |
setItemChildrenFilter (item, is_filter); |
| 343 |
} |
| 344 |
else |
| 345 |
{ |
| 346 |
for (int i = 0; i < item->childCount (); ++i) |
| 347 |
{ |
| 348 |
auto child_item = item->child (i); |
| 349 |
item_stack.push (child_item); |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
void |
| 356 |
Directory::setItemChildrenFilter (QTreeWidgetItem *root, bool is_filter) |
| 357 |
{ |
| 358 |
filterItemBy (root, [is_filter] (const FileIndex *a) -> filterFuncReturn { |
| 359 |
return { is_filter, false }; |
| 360 |
}); |
| 361 |
} |
| 362 |
|
| 363 |
QTreeWidgetItem * |
| 364 |
Directory::findTreeWidgetItem (QTreeWidgetItem *itr, FileIndexType type, |
| 365 |
const string &path, int pn, |
| 366 |
const string &anchor) |
| 367 |
{ |
| 368 |
if (itr == nullptr) |
| 369 |
return nullptr; |
| 370 |
|
| 371 |
auto index = getFileIndexFromTreeItem (itr); |
| 372 |
if (index == nullptr || index->type != type) |
| 373 |
{ |
| 374 |
for (auto ind = 0; ind < itr->childCount (); ++ind) |
| 375 |
{ |
| 376 |
auto child_itr = itr->child (ind); |
| 377 |
auto citr = findTreeWidgetItem (child_itr, type, path, pn, anchor); |
| 378 |
if (citr) |
| 379 |
return citr; |
| 380 |
} |
| 381 |
|
| 382 |
return nullptr; |
| 383 |
} |
| 384 |
|
| 385 |
auto file_index = treeItemToFileIndex (itr); |
| 386 |
if (file_index == nullptr) |
| 387 |
{ |
| 388 |
if (index->page == pn && (anchor.empty () || index->anchor == anchor)) |
| 389 |
{ |
| 390 |
return itr; |
| 391 |
} |
| 392 |
|
| 393 |
return nullptr; |
| 394 |
} |
| 395 |
|
| 396 |
if (file_index->path == path && index->page == pn |
| 397 |
&& (anchor.empty () || index->anchor == anchor)) |
| 398 |
{ |
| 399 |
return itr; |
| 400 |
} |
| 401 |
|
| 402 |
return nullptr; |
| 403 |
} |
| 404 |
|
| 405 |
bool |
| 406 |
Directory::setCurrentIndex (const string &path, int pn, const string &anchor) |
| 407 |
{ |
| 408 |
auto itr = selectedTreeItem (); |
| 409 |
|
| 410 |
auto fitr = findTreeWidgetItem (itr, FileIndexType::PAGE, path, pn, anchor); |
| 411 |
|
| 412 |
if (fitr == nullptr) |
| 413 |
fitr = findTreeWidgetItem (mTreeWidget.invisibleRootItem (), |
| 414 |
FileIndexType::PAGE, path, pn, anchor); |
| 415 |
|
| 416 |
if (fitr == nullptr) |
| 417 |
fitr = findTreeWidgetItem (mTreeWidget.invisibleRootItem (), |
| 418 |
FileIndexType::FILE, path, pn, anchor); |
| 419 |
|
| 420 |
if (fitr) |
| 421 |
{ |
| 422 |
setItemSelected (fitr); |
| 423 |
return true; |
| 424 |
} |
| 425 |
|
| 426 |
return false; |
| 427 |
} |
| 428 |
|
| 429 |
void |
| 430 |
Directory::scrollUp (int times) |
| 431 |
{ |
| 432 |
auto item = selectedTreeItem (); |
| 433 |
if (item == nullptr) |
| 434 |
return; |
| 435 |
|
| 436 |
auto parent = item->parent (); |
| 437 |
if (parent == nullptr) |
| 438 |
{ |
| 439 |
parent = mTreeWidget.invisibleRootItem (); |
| 440 |
} |
| 441 |
|
| 442 |
auto index = parent->indexOfChild (item); |
| 443 |
if (index > 0) |
| 444 |
{ |
| 445 |
auto new_index = index > times ? index - times : 0; |
| 446 |
auto itr = parent->child (new_index); |
| 447 |
setItemSelected (itr); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
void |
| 452 |
Directory::scrollDown (int times) |
| 453 |
{ |
| 454 |
auto item = selectedTreeItem (); |
| 455 |
if (item == nullptr) |
| 456 |
return; |
| 457 |
|
| 458 |
auto parent = item->parent (); |
| 459 |
if (parent == nullptr) |
| 460 |
{ |
| 461 |
parent = mTreeWidget.invisibleRootItem (); |
| 462 |
} |
| 463 |
|
| 464 |
auto index = parent->indexOfChild (item); |
| 465 |
auto count = parent->childCount (); |
| 466 |
auto new_index = index + times < count ? index + times : count - 1; |
| 467 |
auto itr = parent->child (new_index); |
| 468 |
setItemSelected (itr); |
| 469 |
} |
| 470 |
|
| 471 |
void |
| 472 |
Directory::scrollLeft (int times) |
| 473 |
{ |
| 474 |
auto item = selectedTreeItem (); |
| 475 |
if (item == nullptr) |
| 476 |
return; |
| 477 |
|
| 478 |
if (item->parent () == nullptr) |
| 479 |
return; |
| 480 |
|
| 481 |
auto parent = item->parent (); |
| 482 |
|
| 483 |
while (--times > 0 && parent->parent ()) |
| 484 |
{ |
| 485 |
parent = parent->parent (); |
| 486 |
} |
| 487 |
|
| 488 |
setItemSelected (parent); |
| 489 |
} |
| 490 |
|
| 491 |
void |
| 492 |
Directory::scrollRight (int times) |
| 493 |
{ |
| 494 |
auto item = selectedTreeItem (); |
| 495 |
if (item == nullptr) |
| 496 |
return; |
| 497 |
|
| 498 |
if (item->childCount () == 0) |
| 499 |
return; |
| 500 |
|
| 501 |
item = item->child (0); |
| 502 |
|
| 503 |
while (--times > 0 && item->childCount () > 0) |
| 504 |
{ |
| 505 |
item = item->child (0); |
| 506 |
} |
| 507 |
setItemSelected (item); |
| 508 |
} |
| 509 |
|
| 510 |
void |
| 511 |
Directory::onFileRename () |
| 512 |
{ |
| 513 |
auto items = mTreeWidget.selectedItems (); |
| 514 |
if (items.isEmpty ()) |
| 515 |
return; |
| 516 |
|
| 517 |
auto item = items[0]; |
| 518 |
auto index = getFileIndexFromTreeItem (item); |
| 519 |
if (index->type != FileIndexType::FILE) |
| 520 |
return; |
| 521 |
|
| 522 |
auto qpath = QString::fromLocal8Bit (index->path); |
| 523 |
auto text = QString (tr ("Input new name of %1")).arg (qpath); |
| 524 |
auto user_text = QInputDialog::getText (this, tr ("Rename"), text, |
| 525 |
QLineEdit::Normal, qpath); |
| 526 |
auto nname = user_text.trimmed (); |
| 527 |
if (nname.isEmpty ()) |
| 528 |
return; |
| 529 |
|
| 530 |
if (QFile (qpath).rename (nname)) |
| 531 |
{ |
| 532 |
index->path = nname.toStdString (); |
| 533 |
index->title = index->path.substr (index->path.rfind ('/') + 1); |
| 534 |
setFileIndexToTreeItem (item, index); |
| 535 |
} |
| 536 |
else |
| 537 |
{ |
| 538 |
text = QString (tr ("Rename %1 to %2 failed")).arg (qpath).arg (nname); |
| 539 |
QMessageBox::warning (this, tr ("Warning"), text); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
void |
| 544 |
Directory::onFileDelete () |
| 545 |
{ |
| 546 |
auto items = mTreeWidget.selectedItems (); |
| 547 |
if (items.isEmpty ()) |
| 548 |
return; |
| 549 |
|
| 550 |
auto msg_res = QMessageBox::No; |
| 551 |
for (auto item : items) |
| 552 |
{ |
| 553 |
auto index = getFileIndexFromTreeItem (item); |
| 554 |
if (index->type != FileIndexType::FILE) |
| 555 |
continue; |
| 556 |
|
| 557 |
auto qpath = QString::fromLocal8Bit (index->path); |
| 558 |
auto text = QString (tr ("Will delete the \n%1, confirm ?")).arg (qpath); |
| 559 |
if (msg_res == QMessageBox::No || msg_res == QMessageBox::Yes) |
| 560 |
{ |
| 561 |
auto buttons = QMessageBox::Yes | QMessageBox::No; |
| 562 |
if (items.size () > 1) |
| 563 |
buttons = QMessageBox::Yes | QMessageBox::YesToAll |
| 564 |
| QMessageBox::No | QMessageBox::NoToAll; |
| 565 |
msg_res = QMessageBox::question (this, tr ("Confirm"), text, buttons, |
| 566 |
QMessageBox::No); |
| 567 |
} |
| 568 |
|
| 569 |
if (msg_res == QMessageBox::NoToAll) |
| 570 |
return; |
| 571 |
|
| 572 |
else if (msg_res == QMessageBox::No) |
| 573 |
continue; |
| 574 |
|
| 575 |
auto parent = item->parent (); |
| 576 |
if (parent == nullptr) |
| 577 |
{ |
| 578 |
auto offset = mTreeWidget.indexOfTopLevelItem (item); |
| 579 |
mIndex.removeChild (*index); |
| 580 |
item = mTreeWidget.takeTopLevelItem (offset); |
| 581 |
delete item; |
| 582 |
} |
| 583 |
else |
| 584 |
{ |
| 585 |
auto pindex = getFileIndexFromTreeItem (parent); |
| 586 |
pindex->removeChild (*index); |
| 587 |
parent->removeChild (item); |
| 588 |
} |
| 589 |
|
| 590 |
qDebug () << "delete " << qpath; |
| 591 |
QFile::remove (qpath); |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
void |
| 596 |
Directory::onRefresh () |
| 597 |
{ |
| 598 |
refreshIndex (mIndex); |
| 599 |
} |
| 600 |
|
| 601 |
void |
| 602 |
Directory::onFilter () |
| 603 |
{ |
| 604 |
auto root = mTreeWidget.invisibleRootItem (); |
| 605 |
auto cur = mFilterType.currentIndex (); |
| 606 |
auto type = static_cast<FilterType> (cur); |
| 607 |
auto text = mFilterText.text ().trimmed (); |
| 608 |
if (text.isEmpty ()) |
| 609 |
{ |
| 610 |
setItemChildrenFilter (root, true); |
| 611 |
return; |
| 612 |
} |
| 613 |
|
| 614 |
QDateTime datetime; |
| 615 |
qint64 qsize; |
| 616 |
decltype (FileIndex::size) size; |
| 617 |
|
| 618 |
switch (type) |
| 619 |
{ |
| 620 |
case FilterType::Title: |
| 621 |
filterItemBy (root, [text] (const FileIndex *a) -> filterFuncReturn { |
| 622 |
return { a->title.find (text.toStdString ()) != string::npos, false }; |
| 623 |
}); |
| 624 |
break; |
| 625 |
|
| 626 |
case FilterType::FileName: |
| 627 |
filterItemBy (root, [text] (const FileIndex *a) -> filterFuncReturn { |
| 628 |
return { a->type == FileIndexType::FILE |
| 629 |
&& a->title.find (text.toStdString ()) != string::npos, |
| 630 |
true }; |
| 631 |
}); |
| 632 |
break; |
| 633 |
|
| 634 |
case FilterType::MTimeBe: |
| 635 |
datetime = QDateTime::fromString (text); |
| 636 |
filterItemBy (root, [datetime] (const FileIndex *a) -> filterFuncReturn { |
| 637 |
return { a->type == FileIndexType::FILE |
| 638 |
&& a->mtime >= datetime.toMSecsSinceEpoch (), |
| 639 |
true }; |
| 640 |
}); |
| 641 |
break; |
| 642 |
|
| 643 |
case FilterType::MTimeLe: |
| 644 |
datetime = QDateTime::fromString (text); |
| 645 |
filterItemBy (root, [datetime] (const FileIndex *a) -> filterFuncReturn { |
| 646 |
return { a->type == FileIndexType::FILE |
| 647 |
&& a->mtime <= datetime.toMSecsSinceEpoch (), |
| 648 |
true }; |
| 649 |
}); |
| 650 |
break; |
| 651 |
|
| 652 |
case FilterType::FileSizeBe: |
| 653 |
qsize = parseFormattedDataSize (text); |
| 654 |
size = static_cast<decltype (size)> (qsize); |
| 655 |
filterItemBy (root, [size] (const FileIndex *a) -> filterFuncReturn { |
| 656 |
return { a->type == FileIndexType::FILE && a->size >= size, true }; |
| 657 |
}); |
| 658 |
break; |
| 659 |
|
| 660 |
case FilterType::FileSizeLe: |
| 661 |
qsize = parseFormattedDataSize (text); |
| 662 |
size = static_cast<decltype (size)> (qsize); |
| 663 |
filterItemBy (root, [size] (const FileIndex *a) -> filterFuncReturn { |
| 664 |
return { a->type == FileIndexType::FILE && a->size <= size, true }; |
| 665 |
}); |
| 666 |
break; |
| 667 |
default: |
| 668 |
qWarning () << tr ("Filter Type is invalid"); |
| 669 |
break; |
| 670 |
} |
| 671 |
|
| 672 |
mTreeWidget.expandAll (); |
| 673 |
} |
| 674 |
|
| 675 |
void |
| 676 |
Directory::sortItems (QTreeWidgetItem *tree_iter) |
| 677 |
{ |
| 678 |
stack<QTreeWidgetItem *> needSort; |
| 679 |
needSort.push (tree_iter); |
| 680 |
|
| 681 |
using itemState = pair<QTreeWidgetItem *, bool>; |
| 682 |
while (!needSort.empty ()) |
| 683 |
{ |
| 684 |
auto root = needSort.top (); |
| 685 |
needSort.pop (); |
| 686 |
|
| 687 |
vector<itemState> item_list; |
| 688 |
for (auto i = 0; i < root->childCount (); ++i) |
| 689 |
{ |
| 690 |
auto item = root->child (i); |
| 691 |
auto index = getFileIndexFromTreeItem (item); |
| 692 |
if (index->type != FileIndexType::PAGE) |
| 693 |
item_list.emplace_back (item, item->isExpanded ()); |
| 694 |
} |
| 695 |
|
| 696 |
std::ranges::for_each (item_list, [this, &needSort] (itemState &p) { |
| 697 |
if (p.first->childCount () > 1) |
| 698 |
{ |
| 699 |
auto index = getFileIndexFromTreeItem (p.first); |
| 700 |
if (index && index->type == FileIndexType::DIR) |
| 701 |
{ |
| 702 |
needSort.push (p.first); |
| 703 |
} |
| 704 |
} |
| 705 |
}); |
| 706 |
|
| 707 |
if (mSortAscending) |
| 708 |
{ |
| 709 |
switch (mSortColumn) |
| 710 |
{ |
| 711 |
using enum Column; |
| 712 |
case Title: |
| 713 |
std::ranges::sort ( |
| 714 |
item_list, std::ranges::greater (), [this] (itemState &p) { |
| 715 |
auto index = getFileIndexFromTreeItem (p.first); |
| 716 |
return index->title; |
| 717 |
}); |
| 718 |
break; |
| 719 |
case MTime: |
| 720 |
std::ranges::sort ( |
| 721 |
item_list, std::ranges::greater (), [this] (itemState &p) { |
| 722 |
auto index = getFileIndexFromTreeItem (p.first); |
| 723 |
return index->mtime; |
| 724 |
}); |
| 725 |
break; |
| 726 |
case FileSize: |
| 727 |
std::ranges::sort ( |
| 728 |
item_list, std::ranges::greater (), [this] (itemState &p) { |
| 729 |
auto index = getFileIndexFromTreeItem (p.first); |
| 730 |
return index->size; |
| 731 |
}); |
| 732 |
break; |
| 733 |
} |
| 734 |
} |
| 735 |
else |
| 736 |
{ |
| 737 |
switch (mSortColumn) |
| 738 |
{ |
| 739 |
using enum Column; |
| 740 |
case Title: |
| 741 |
std::ranges::sort ( |
| 742 |
item_list, std::ranges::less (), [this] (itemState &p) { |
| 743 |
auto index = getFileIndexFromTreeItem (p.first); |
| 744 |
return index->title; |
| 745 |
}); |
| 746 |
break; |
| 747 |
case MTime: |
| 748 |
std::ranges::sort ( |
| 749 |
item_list, std::ranges::less (), [this] (itemState &p) { |
| 750 |
auto index = getFileIndexFromTreeItem (p.first); |
| 751 |
return index->mtime; |
| 752 |
}); |
| 753 |
break; |
| 754 |
case FileSize: |
| 755 |
std::ranges::sort ( |
| 756 |
item_list, std::ranges::less (), [this] (itemState &p) { |
| 757 |
auto index = getFileIndexFromTreeItem (p.first); |
| 758 |
return index->size; |
| 759 |
}); |
| 760 |
break; |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
mTreeWidget.setUpdatesEnabled (false); |
| 765 |
for (auto i = 0; |
| 766 |
static_cast<decltype (item_list.size ())> (i) < item_list.size (); |
| 767 |
++i) |
| 768 |
{ |
| 769 |
auto p = item_list[i]; |
| 770 |
auto oi = root->indexOfChild (p.first); |
| 771 |
if (oi != i) |
| 772 |
{ |
| 773 |
root->takeChild (oi); |
| 774 |
root->insertChild (i, p.first); |
| 775 |
p.first->setExpanded (p.second); |
| 776 |
} |
| 777 |
} |
| 778 |
mTreeWidget.setUpdatesEnabled (true); |
| 779 |
} |
| 780 |
|
| 781 |
mTreeWidget.update (); |
| 782 |
} |
| 783 |
|
| 784 |
void |
| 785 |
Directory::onRowActivated ([[maybe_unused]] QTreeWidgetItem *item, |
| 786 |
[[maybe_unused]] int column) |
| 787 |
{ |
| 788 |
mFrame->directoryShowPage (currentItemFileIndex (), true); |
| 789 |
mFrame->toggledControlDirectory (true); |
| 790 |
} |
| 791 |
|
| 792 |
void |
| 793 |
Directory::onRowDoubleClicked () |
| 794 |
{ |
| 795 |
parentWidget ()->setFocus (); |
| 796 |
} |
| 797 |
|
| 798 |
void |
| 799 |
Directory::onContextMenuRequest (const QPoint &point) |
| 800 |
{ |
| 801 |
auto items = mTreeWidget.selectedItems (); |
| 802 |
if (items.isEmpty ()) |
| 803 |
return; |
| 804 |
|
| 805 |
auto item = items[0]; |
| 806 |
auto index = getFileIndexFromTreeItem (item); |
| 807 |
if (index->type == FileIndexType::FILE) |
| 808 |
{ |
| 809 |
mItemMenu.clear (); |
| 810 |
if (items.size () == 1) |
| 811 |
{ |
| 812 |
auto rename_action = mItemMenu.addAction (tr ("Rename File")); |
| 813 |
QObject::connect (rename_action, SIGNAL (triggered (bool)), this, |
| 814 |
SLOT (onFileRename ())); |
| 815 |
} |
| 816 |
if (std::all_of (items.begin (), items.end (), |
| 817 |
[this] (QTreeWidgetItem *a) { |
| 818 |
auto i = getFileIndexFromTreeItem (a); |
| 819 |
return i && i->type == FileIndexType::FILE; |
| 820 |
})) |
| 821 |
{ |
| 822 |
auto del_action = mItemMenu.addAction (tr ("Delete File")); |
| 823 |
del_action->setIcon ( |
| 824 |
QIcon::fromTheme (QIcon::ThemeIcon::EditDelete)); |
| 825 |
QObject::connect (del_action, SIGNAL (triggered (bool)), this, |
| 826 |
SLOT (onFileDelete ())); |
| 827 |
} |
| 828 |
mItemMenu.popup (mTreeWidget.mapToGlobal (point)); |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
void |
| 833 |
Directory::selectFirstItem () |
| 834 |
{ |
| 835 |
if (setCurrentIndex (mFrame->filename (), mFrame->pageNumber (), "")) |
| 836 |
return; |
| 837 |
|
| 838 |
if (mTreeWidget.topLevelItemCount () > 0) |
| 839 |
{ |
| 840 |
auto itr = mTreeWidget.topLevelItem (0); |
| 841 |
setItemSelected (itr); |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
FileIndex * |
| 846 |
Directory::currentItemFileIndex () |
| 847 |
{ |
| 848 |
auto item = selectedTreeItem (); |
| 849 |
auto index = getFileIndexFromTreeItem (item); |
| 850 |
return index; |
| 851 |
} |
| 852 |
|
| 853 |
FileIndex * |
| 854 |
Directory::currentFileFileIndex () |
| 855 |
{ |
| 856 |
auto item = selectedTreeItem (); |
| 857 |
auto index = treeItemToFileIndex (item); |
| 858 |
return index; |
| 859 |
} |
| 860 |
|
| 861 |
} |
| 862 |
|
| 863 |
// Local Variables: |
| 864 |
// mode: c++ |
| 865 |
// End: |