-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsession.cpp
More file actions
210 lines (178 loc) · 4.98 KB
/
session.cpp
File metadata and controls
210 lines (178 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <QFileInfo>
#include <QDebug>
#include "session.h"
#include "mainwindow.h"
// Function used to sort strings where whole numbers present in the string are compared
// and not seperate digits
bool lessThanObeyingNumericOrder(const QString lhs, const QString rhs)
{
int lhsLength = lhs.length();
int rhsLength = rhs.length();
int currentLhsCharPos = 0;
int currentRhsCharPos = 0;
while (currentLhsCharPos < lhsLength && currentRhsCharPos < rhsLength)
{
QChar lhsChar = lhs.at(currentLhsCharPos);
QChar rhsChar = rhs.at(currentRhsCharPos);
// If both characters are digits find the full number and compare the two
if (lhsChar.isDigit() && rhsChar.isDigit())
{
QString lhsNum = lhsChar;
while (lhs.at(currentLhsCharPos + 1).isDigit())
{
currentLhsCharPos++;
lhsNum += lhs.at(currentLhsCharPos);
}
QString rhsNum = rhsChar;
while (rhs.at(currentRhsCharPos + 1).isDigit())
{
currentRhsCharPos++;
rhsNum += rhs.at(currentRhsCharPos);
}
if (lhsNum.toInt() != rhsNum.toInt())
{
return lhsNum.toInt() < rhsNum.toInt();
}
}
else
{
if (lhsChar != rhsChar)
{
return lhsChar < rhsChar;
}
}
currentLhsCharPos++;
currentRhsCharPos++;
}
return false;
}
Session::Session(QString descriptor)
{
QStringList items = descriptor.split(";");
name = items.at(0);
QString imgFilename = items.at(1);
currentFileName = imgFilename;
QFileInfo imgInfo(imgFilename);
imgDirectory = imgInfo.absoluteDir();
currentFileIt = fileNames.begin();
QString txtPath = items.at(2);
hasTextDirectory = !txtPath.isEmpty();
if (hasTextDirectory)
{
QFileInfo txtInfo(txtPath);
txtDirectory = txtInfo.absoluteFilePath();
}
LoadFiles();
}
QString Session::GetImgDirectoryPath()
{
return imgDirectory.absolutePath();
}
QString Session::GetName()
{
return name;
}
QString Session::GetCurrentFileName()
{
return currentFileName;
}
QString Session::GetCurrentHoverText()
{
QFileInfo imageInfo(currentFileName);
QString baseName = imageInfo.baseName();
QString textPath = txtDirectory.absolutePath() + QDir::separator() + baseName + ".txt";
QFileInfo textInfo(textPath);
if (textInfo.exists() && textInfo.isFile())
{
QFile textFile(textPath);
if (textFile.open(QIODevice::ReadOnly))
{
QTextStream stream(&textFile);
QString hoverText = stream.readAll();
return hoverText;
}
}
// If we're using an ordered symlink directory from dosage the
// image file will have it's index prepended to the filename, eg:
// 00023_23rdstrip.png
// Try find a text match without that index trimmed off
QString trimmedBaseName = baseName.mid(6);
textPath = txtDirectory.absolutePath() + QDir::separator() + trimmedBaseName + ".txt";
textInfo = QFileInfo(textPath);
if (textInfo.exists() && textInfo.isFile())
{
QFile textFile(textPath);
if (textFile.open(QIODevice::ReadOnly))
{
QTextStream stream(&textFile);
QString hoverText = stream.readAll();
return hoverText;
}
}
return "";
}
QString Session::GetDescriptor()
{
QString descriptor = name + ";" + currentFileName + ";";
if (hasTextDirectory)
{
descriptor += txtDirectory.absolutePath();
}
return descriptor;
}
void Session::LoadFiles()
{
fileNames = GetSortedFilenameList();
currentFileIt = fileNames.begin();
while (*currentFileIt != currentFileName)
{
currentFileIt++;
}
}
void Session::NextFile()
{
currentFileIt++;
if (currentFileIt == fileNames.end())
{
currentFileIt--;
}
currentFileName = *currentFileIt;
MainWindow::Get()->PopulateSessionList();
}
void Session::PreviousFile()
{
if (currentFileIt != fileNames.begin())
{
currentFileIt--;
}
currentFileName = *currentFileIt;
MainWindow::Get()->PopulateSessionList();
}
void Session::FirstFile()
{
currentFileIt = fileNames.begin();
currentFileName = *currentFileIt;
MainWindow::Get()->PopulateSessionList();
}
void Session::LastFile()
{
currentFileIt = fileNames.end();
currentFileIt--;
currentFileName = *currentFileIt;
MainWindow::Get()->PopulateSessionList();
}
bool Session::IsOnLastFile()
{
return currentFileName == fileNames.last();
}
QStringList Session::GetSortedFilenameList()
{
QStringList theFilenames;
QFileInfoList fileInfos = imgDirectory.entryInfoList(QDir::Files);
for (auto info: fileInfos)
{
theFilenames.push_back(info.absoluteFilePath());
}
qSort(theFilenames.begin(), theFilenames.end(), lessThanObeyingNumericOrder);
return theFilenames;
}