Debian Codegraph @

src/gui/captureHandler/CaptureTabStateHandler.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 Damir Porobic <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "CaptureTabStateHandler.h"
21
22 CaptureTabStateHandler::CaptureTabStateHandler() :
23 mCurrentTabIndex(-1)
24 {
25 }
26
27 void CaptureTabStateHandler::add(int index, const QString &filename, const QString &path, bool isSaved)
28 {
29 auto newState = QSharedPointer<CaptureTabState>(new CaptureTabState(index, filename, path, isSaved));
30 mCaptureTabStates.append(newState);
31 refreshTabInfo(index);
32 }
33
34 bool CaptureTabStateHandler::isSaved(int index)
35 {
36 auto tabState = getTabState(index);
37 return tabState.isNull() || tabState->isSaved;
38 }
39
40 bool CaptureTabStateHandler::isPathValid(int index)
41 {
42 return PathHelper::isPathValid(path(index));
43 }
44
45 QString CaptureTabStateHandler::path(int index)
46 {
47 auto tabState = getTabState(index);
48 return tabState.isNull() ? QString() : tabState->path;
49 }
50
51 QString CaptureTabStateHandler::filename(int index)
52 {
53 auto tabState = getTabState(index);
54 return tabState.isNull() ? QString() : tabState->filename;
55 }
56
57 void CaptureTabStateHandler::setSaveState(int index, const SaveResultDto &saveResult)
58 {
59 auto tabState = getTabState(index);
60 if (!tabState.isNull()) {
61 tabState->isSaved = saveResult.isSuccessful;
62
63 if (saveResult.isSuccessful) {
64 tabState->path = saveResult.path;
65 tabState->filename = PathHelper::extractFilename(saveResult.path);
66 refreshTabInfo(index);
67 }
68 }
69 }
70
71 void CaptureTabStateHandler::renameFile(int index, const RenameResultDto &renameResult)
72 {
73 auto tabState = getTabState(index);
74 if (!tabState.isNull() && renameResult.isSuccessful) {
75 tabState->path = renameResult.path;
76 tabState->filename = PathHelper::extractFilename(renameResult.path);
77 refreshTabInfo(index);
78 }
79 }
80
81 void CaptureTabStateHandler::tabMoved(int fromIndex, int toIndex)
82 {
83 for (auto& state : mCaptureTabStates) {
84 if(state->index == fromIndex) {
85 state->index = toIndex;
86 } else if(state->index == toIndex) {
87 state->index = fromIndex;
88 }
89 }
90 }
91
92 void CaptureTabStateHandler::currentTabChanged(int index)
93 {
94 mCurrentTabIndex = index;
95 }
96
97 void CaptureTabStateHandler::tabRemoved(int index)
98 {
99 auto iterator = mCaptureTabStates.begin();
100 while (iterator != mCaptureTabStates.end()) {
101 if (iterator->data()->index == index) {
102 iterator = mCaptureTabStates.erase(iterator);
103 } else {
104 if (iterator->data()->index > index) {
105 iterator->data()->index--;
106 }
107 iterator++;
108 }
109 }
110 }
111
112 void CaptureTabStateHandler::currentTabContentChanged()
113 {
114 auto currentTabState = getTabState(mCurrentTabIndex);
115 if (!currentTabState.isNull()) {
116 currentTabState->isSaved = false;
117 refreshTabInfo(mCurrentTabIndex);
118 }
119 }
120
121 void CaptureTabStateHandler::refreshTabInfo(int index)
122 {
123 auto tabState = getTabState(index);
124 if (!tabState.isNull()) {
125 auto title = tabState->isSaved ? tabState->filename : tabState->filename + QLatin1String("*");
126 emit updateTabInfo(tabState->index, title, tabState->path);
127 }
128 }
129
130 int CaptureTabStateHandler::count() const
131 {
132 return mCaptureTabStates.count();
133 }
134
135 int CaptureTabStateHandler::currentTabIndex() const
136 {
137 return mCurrentTabIndex;
138 }
139
140 QSharedPointer<CaptureTabState> CaptureTabStateHandler::getTabState(int index)
141 {
142 for (auto &state : mCaptureTabStates) {
143 if (state->index == index) {
144 return state;
145 }
146 }
147 return QSharedPointer<CaptureTabState>(nullptr);
148 }