| 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.h |
| 24 |
* |
| 25 |
* Author: Alf <[email protected]> |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifndef _APVLV_CONTENT_H_ |
| 29 |
#define _APVLV_CONTENT_H_ |
| 30 |
|
| 31 |
#include <QComboBox> |
| 32 |
#include <QMenu> |
| 33 |
#include <QTimer> |
| 34 |
#include <QToolBar> |
| 35 |
#include <QTreeWidgetItem> |
| 36 |
#include <QVBoxLayout> |
| 37 |
#include <iostream> |
| 38 |
#include <map> |
| 39 |
#include <string> |
| 40 |
|
| 41 |
#include "ApvlvFile.h" |
| 42 |
#include "ApvlvUtil.h" |
| 43 |
#include "ApvlvWidget.h" |
| 44 |
|
| 45 |
namespace apvlv |
| 46 |
{ |
| 47 |
|
| 48 |
class ContentTree : public QTreeWidget |
| 49 |
{ |
| 50 |
protected: |
| 51 |
void keyPressEvent (QKeyEvent *event) override; |
| 52 |
}; |
| 53 |
|
| 54 |
class ApvlvFrame; |
| 55 |
class Directory final : public QFrame |
| 56 |
{ |
| 57 |
Q_OBJECT |
| 58 |
public: |
| 59 |
Directory (); |
| 60 |
|
| 61 |
~Directory () override = default; |
| 62 |
|
| 63 |
bool isReady (); |
| 64 |
|
| 65 |
enum class Column : int |
| 66 |
{ |
| 67 |
Title = 0, |
| 68 |
MTime, |
| 69 |
FileSize, |
| 70 |
}; |
| 71 |
static std::vector<const char *> ColumnString; |
| 72 |
static std::vector<const char *> SortByColumnString; |
| 73 |
|
| 74 |
enum class FilterType : int |
| 75 |
{ |
| 76 |
Title = 0, |
| 77 |
FileName, |
| 78 |
MTimeBe, |
| 79 |
MTimeLe, |
| 80 |
FileSizeBe, |
| 81 |
FileSizeLe, |
| 82 |
}; |
| 83 |
static std::vector<const char *> FilterTypeString; |
| 84 |
|
| 85 |
FileIndex *currentItemFileIndex (); |
| 86 |
|
| 87 |
FileIndex *currentFileFileIndex (); |
| 88 |
|
| 89 |
QTreeWidgetItem *findTreeWidgetItem (QTreeWidgetItem *itr, |
| 90 |
FileIndexType type, |
| 91 |
const std::string &path, int pn, |
| 92 |
const std::string &anchor); |
| 93 |
|
| 94 |
bool setCurrentIndex (const std::string &path, int pn, |
| 95 |
const std::string &anchor); |
| 96 |
|
| 97 |
void |
| 98 |
setFrame (ApvlvFrame *frame) |
| 99 |
{ |
| 100 |
mFrame = frame; |
| 101 |
} |
| 102 |
|
| 103 |
void |
| 104 |
focusFilter () |
| 105 |
{ |
| 106 |
QTimer::singleShot (50, &mFilterText, SLOT (setFocus ())); |
| 107 |
} |
| 108 |
|
| 109 |
void scrollUp (int times); |
| 110 |
|
| 111 |
void scrollDown (int times); |
| 112 |
|
| 113 |
void scrollLeft (int times); |
| 114 |
|
| 115 |
void scrollRight (int times); |
| 116 |
|
| 117 |
void |
| 118 |
setActive (bool active) |
| 119 |
{ |
| 120 |
if (active) |
| 121 |
{ |
| 122 |
QTimer::singleShot (50, &mTreeWidget, SLOT (setFocus ())); |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
mTreeWidget.clearFocus (); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
bool |
| 131 |
isActive () |
| 132 |
{ |
| 133 |
return mTreeWidget.hasFocus (); |
| 134 |
} |
| 135 |
|
| 136 |
private: |
| 137 |
QVBoxLayout mLayout; |
| 138 |
QToolBar mToolBar; |
| 139 |
ApvlvLineEdit mFilterText; |
| 140 |
QComboBox mFilterType; |
| 141 |
QComboBox mSortType; |
| 142 |
ContentTree mTreeWidget; |
| 143 |
|
| 144 |
QMenu mItemMenu; |
| 145 |
|
| 146 |
std::map<FileIndexType, QIcon> mTypeIcons; |
| 147 |
|
| 148 |
FileIndex mIndex; |
| 149 |
Column mSortColumn{ Column::Title }; |
| 150 |
|
| 151 |
ApvlvFrame *mFrame{ nullptr }; |
| 152 |
|
| 153 |
bool mSortAscending{ true }; |
| 154 |
|
| 155 |
void setupToolBar (); |
| 156 |
void setupTree (); |
| 157 |
|
| 158 |
QTreeWidgetItem * |
| 159 |
selectedTreeItem () |
| 160 |
{ |
| 161 |
auto selitems = mTreeWidget.selectedItems (); |
| 162 |
return selitems.isEmpty () ? nullptr : selitems[0]; |
| 163 |
} |
| 164 |
void setItemSelected (QTreeWidgetItem *item); |
| 165 |
|
| 166 |
void setIndex (FileIndex &index, QTreeWidgetItem *root_itr); |
| 167 |
void refreshIndex (const FileIndex &index); |
| 168 |
|
| 169 |
void setFileIndexToTreeItem (QTreeWidgetItem *item, FileIndex *index); |
| 170 |
FileIndex *getFileIndexFromTreeItem (QTreeWidgetItem *item); |
| 171 |
|
| 172 |
FileIndex *treeItemToFileIndex (QTreeWidgetItem *item); |
| 173 |
|
| 174 |
using filterFuncReturn = std::tuple<bool, bool>; |
| 175 |
using filterFunc = std::function<filterFuncReturn (const FileIndex *)>; |
| 176 |
void filterItemBy (QTreeWidgetItem *root, const filterFunc &filter_func); |
| 177 |
void setItemChildrenFilter (QTreeWidgetItem *root, bool is_filter); |
| 178 |
|
| 179 |
private slots: |
| 180 |
void onFileRename (); |
| 181 |
void onFileDelete (); |
| 182 |
void onRefresh (); |
| 183 |
void onFilter (); |
| 184 |
void |
| 185 |
sortBy (int method) |
| 186 |
{ |
| 187 |
mSortAscending = !mSortAscending; |
| 188 |
mSortColumn = static_cast<Column> (method); |
| 189 |
sortItems (mTreeWidget.invisibleRootItem ()); |
| 190 |
} |
| 191 |
|
| 192 |
void sortItems (QTreeWidgetItem *root); |
| 193 |
|
| 194 |
void onRowActivated (QTreeWidgetItem *item, int column); |
| 195 |
void onRowDoubleClicked (); |
| 196 |
void onContextMenuRequest (const QPoint &point); |
| 197 |
void selectFirstItem (); |
| 198 |
void setIndex (const FileIndex &index); |
| 199 |
}; |
| 200 |
} |
| 201 |
|
| 202 |
#endif |
| 203 |
|
| 204 |
/* Local Variables: */ |
| 205 |
/* mode: c++ */ |
| 206 |
/* End: */ |