This commit is contained in:
Luis Ángel San Martín
2015-12-12 13:20:25 +01:00
18 changed files with 397 additions and 290 deletions

View File

@ -15,6 +15,17 @@
#include "QsLog.h"
enum YACReaderPageSortingMode
{
YACReaderNumericalSorting,
YACReaderHeuristicSorting,
YACReaderAlphabeticalSorting
};
void comic_pages_sort(QList<QString> & pageNames, YACReaderPageSortingMode sortingMode);
const QStringList Comic::imageExtensions = QStringList() << "*.jpg" << "*.jpeg" << "*.png" << "*.gif" << "*.tiff" << "*.tif" << "*.bmp" << "*.webp";
const QStringList Comic::literalImageExtensions = QStringList() << "jpg" << "jpeg" << "png" << "gif" << "tiff" << "tif" << "bmp" << "webp";
@ -511,7 +522,9 @@ void FileComic::process()
_loaded = true;
_cfi=0;
qSort(_fileNames.begin(),_fileNames.end(), naturalSortLessThanCI);
//TODO, add a setting for choosing the type of page sorting used.
comic_pages_sort(_fileNames, YACReaderHeuristicSorting);
if(_firstPage == -1)
_firstPage = bm->getLastPage();
@ -587,7 +600,8 @@ void FolderComic::process()
//d.setSorting(QDir::Name|QDir::IgnoreCase|QDir::LocaleAware);
QFileInfoList list = d.entryInfoList();
qSort(list.begin(),list.end(),naturalSortLessThanCIFileInfo);
//don't fix double page files sorting, because the user can see how the SO sorts the files in the folder.
std::sort(list.begin(),list.end(),naturalSortLessThanCIFileInfo);
int nPages = list.size();
_pages.clear();
@ -825,3 +839,167 @@ Comic * FactoryComic::newComic(const QString & path)
return NULL;
}
bool is_double_page(const QString & pageName, const QString & commonPrefix, const int maxExpectedDoublePagesNumberLenght)
{
if(pageName.startsWith(commonPrefix))
{
QString substringContainingPageNumbers = pageName.mid(commonPrefix.length());
QString pageNumbersSubString;
for(int i = 0 ; i < substringContainingPageNumbers.length() && substringContainingPageNumbers.at(i).isDigit(); i++)
pageNumbersSubString.append(substringContainingPageNumbers.at(i));
if(pageNumbersSubString.length() < 3 || pageNumbersSubString.length() > maxExpectedDoublePagesNumberLenght || pageNumbersSubString.length() % 2 == 1)
return false;
int leftPageNumber = pageNumbersSubString.left(pageNumbersSubString.length() / 2).toInt();
int rightPageNumber = pageNumbersSubString.mid(pageNumbersSubString.length() / 2).toInt();
if(leftPageNumber == 0 || rightPageNumber == 0)
return false;
if((rightPageNumber - leftPageNumber) == 1)
return true;
}
return false;
}
QString get_most_common_prefix(const QList<QString> & pageNames)
{
if(pageNames.isEmpty())
return "";
QMap<QString, uint> frequency;
int currentPrefixLenght = pageNames.at(0).split('/').last().length();
int currentPrefixCount = 1;
int i;
QString previous;
QString current;
for(i = 1; i < pageNames.length(); i++)
{
int pos = 0;
previous = pageNames.at(i-1).split('/').last();
current = pageNames.at(i).split('/').last();
for(; pos < current.length() && previous[pos] == current[pos]; pos++);
if(pos < currentPrefixLenght && pos > 0)
{
frequency.insert(previous.left(currentPrefixLenght), currentPrefixCount);
currentPrefixLenght = pos;
currentPrefixCount++;
}
/*
else if(pos > currentPrefixLenght)
{
frequency.insert(pageNames.at(i-1).left(currentPrefixLenght), currentPrefixCount - 1);
currentPrefixLenght = pos;
currentPrefixCount = 2;
}*/
else if(pos == 0)
{
frequency.insert(previous.left(currentPrefixLenght), currentPrefixCount);
currentPrefixLenght = current.length();
currentPrefixCount = 1;
}
else
currentPrefixCount++;
}
frequency.insert(previous.left(currentPrefixLenght), currentPrefixCount);
uint maxFrequency = 0;
QString common_prefix = "";
foreach(QString key, frequency.keys())
{
if(maxFrequency < frequency.value(key))
{
maxFrequency = frequency.value(key);
common_prefix = key;
}
}
QRegExp allNumberRegExp("\\d+");
if (allNumberRegExp.exactMatch(common_prefix))
return "";
if(maxFrequency < pageNames.length() * 0.60) //the most common tipe of image file should a proper page, so we can asume that the common_prefix should be in, at least, the 60% of the pages
return "";
return common_prefix;
}
void get_double_pages(const QList<QString> & pageNames, QList<QString> & singlePageNames/*out*/, QList<QString> & doublePageNames/*out*/)
{
uint maxExpectedDoublePagesNumberLenght = (int)(log10(pageNames.length())+1) * 2;
QString mostCommonPrefix = get_most_common_prefix(pageNames);
foreach(const QString & pageName, pageNames)
{
if(is_double_page(pageName.split('/').last(), mostCommonPrefix, maxExpectedDoublePagesNumberLenght))
doublePageNames.append(pageName);
else
singlePageNames.append(pageName);
}
}
QList<QString> merge_pages(QList<QString> & singlePageNames, QList<QString> & doublePageNames)
{
//NOTE: this implementation doesn't differ from std::merge using a custom comparator, but it can be easily tweaked if merging requeries an additional heuristic behaviour
QList<QString> pageNames;
int i = 0;
int j = 0;
while (i < singlePageNames.length() && j < doublePageNames.length())
{
if (singlePageNames.at(i).compare(doublePageNames.at(j), Qt::CaseInsensitive) < 0)
pageNames.append(singlePageNames.at(i++));
else
pageNames.append(doublePageNames.at(j++));
}
while (i < singlePageNames.length())
pageNames.append(singlePageNames.at(i++));
while (j < doublePageNames.length())
pageNames.append(doublePageNames.at(j++));
return pageNames;
}
void comic_pages_sort(QList<QString> & pageNames, YACReaderPageSortingMode sortingMode)
{
switch(sortingMode)
{
case YACReaderNumericalSorting:
std::sort(pageNames.begin(), pageNames.end(), naturalSortLessThanCI);
break;
case YACReaderHeuristicSorting:
{
std::sort(pageNames.begin(), pageNames.end(), naturalSortLessThanCI);
QList<QString> singlePageNames;
QList<QString> doublePageNames;
get_double_pages(pageNames, singlePageNames, doublePageNames);
if(doublePageNames.length() > 0)
{
pageNames = merge_pages(singlePageNames, doublePageNames);
}
break;
}
case YACReaderAlphabeticalSorting:
std::sort(pageNames.begin(), pageNames.end());
break;
}
}

View File

@ -325,6 +325,7 @@ void YACReaderFlowGL::paintGL()
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(numObjects>0)
@ -535,22 +536,22 @@ void YACReaderFlowGL::drawCover(const YACReader3DImage & image)
//esquina inferior izquierda
glColor4f(RUP*opacity,RUP*opacity,RUP*opacity,1);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(w/2.f-0.2, -0.685f+h, 0.001f);
glVertex3f(w/2.f-0.2, -0.688f+h, 0.001f);
//esquina inferior derecha
glColor4f(RUP*opacity,RUP*opacity,RUP*opacity,1);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(w/2.f-0.05, -0.685f+h, 0.001f);
glVertex3f(w/2.f-0.05, -0.688f+h, 0.001f);
//esquina superior derecha
glColor4f(RUP*opacity,RUP*opacity,RUP*opacity,1);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(w/2.f-0.05, -0.485f+h, 0.001f);
glVertex3f(w/2.f-0.05, -0.488f+h, 0.001f);
//esquina superior izquierda
glColor4f(RUP*opacity,RUP*opacity,RUP*opacity,1);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(w/2.f-0.2, -0.485f+h, 0.001f);
glVertex3f(w/2.f-0.2, -0.488f+h, 0.001f);
glEnd();
glDisable(GL_TEXTURE_2D);

View File

@ -309,8 +309,8 @@ void YACReaderFlowGL::initializeGL()
void YACReaderFlowGL::paintGL()
{
/*glClearDepth(1.0);
glClearColor(1,1,1,1);*/
/*glClearDepth(1.0);*/
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/*glLoadIdentity();
glTranslatef(0.0, 0.0, -10.0);
@ -505,22 +505,22 @@ void YACReaderFlowGL::drawCover(const YACReader3DImage & image)
//esquina inferior izquierda
glColor4f(RUP*opacity,RUP*opacity,RUP*opacity,1);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(w/2.f-0.2, -0.685f+h, 0.001f);
glVertex3f(w/2.f-0.2, -0.688f+h, 0.001f);
//esquina inferior derecha
glColor4f(RUP*opacity,RUP*opacity,RUP*opacity,1);
glColor4f(RUP*opacity,RUP*opacity,RUP*opacity,1);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(w/2.f-0.05, -0.685f+h, 0.001f);
glVertex3f(w/2.f-0.05, -0.688f+h, 0.001f);
//esquina superior derecha
glColor4f(RUP*opacity,RUP*opacity,RUP*opacity,1);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(w/2.f-0.05, -0.485f+h, 0.001f);
glVertex3f(w/2.f-0.05, -0.488f+h, 0.001f);
//esquina superior izquierda
glColor4f(RUP*opacity,RUP*opacity,RUP*opacity,1);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(w/2.f-0.2, -0.485f+h, 0.001f);
glVertex3f(w/2.f-0.2, -0.488f+h, 0.001f);
glEnd();
glDisable(GL_TEXTURE_2D);

View File

@ -1,245 +1,15 @@
/* This file contains parts of the KDE libraries
Copyright (C) 1999 Ian Zepp (icszepp@islc.net)
Copyright (C) 2006 by Dominic Battre <dominic@battre.de>
Copyright (C) 2006 by Martin Pool <mbp@canonical.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "qnaturalsorting.h"
//from KDE
/*
int naturalCompare(const QString &_a, const QString &_b, Qt::CaseSensitivity caseSensitivity)
#include <QCollator>
int naturalCompare(const QString &s1, const QString &s2, Qt::CaseSensitivity caseSensitivity)
{
// This method chops the input a and b into pieces of
// digits and non-digits (a1.05 becomes a | 1 | . | 05)
// and compares these pieces of a and b to each other
// (first with first, second with second, ...).
//
// This is based on the natural sort order code code by Martin Pool
// http://sourcefrog.net/projects/natsort/
// Martin Pool agreed to license this under LGPL or GPL.
// FIXME: Using toLower() to implement case insensitive comparison is
// sub-optimal, but is needed because we compare strings with
// localeAwareCompare(), which does not know about case sensitivity.
// A task has been filled for this in Qt Task Tracker with ID 205990.
// http://trolltech.com/developer/task-tracker/index_html?method=entry&id=205990
QString a;
QString b;
if (caseSensitivity == Qt::CaseSensitive) {
a = _a;
b = _b;
} else {
a = _a.toLower();
b = _b.toLower();
}
const QChar* currA = a.unicode(); // iterator over a
const QChar* currB = b.unicode(); // iterator over b
if (currA == currB) {
return 0;
}
const QChar* begSeqA = currA; // beginning of a new character sequence of a
const QChar* begSeqB = currB;
while (!currA->isNull() && !currB->isNull()) {
if (currA->unicode() == QChar::ObjectReplacementCharacter) {
return 1;
}
if (currB->unicode() == QChar::ObjectReplacementCharacter) {
return -1;
}
if (currA->unicode() == QChar::ReplacementCharacter) {
return 1;
}
if (currB->unicode() == QChar::ReplacementCharacter) {
return -1;
}
// find sequence of characters ending at the first non-character
while (!currA->isNull() && !currA->isDigit() && !currA->isPunct() && !currA->isSpace()) {
++currA;
}
while (!currB->isNull() && !currB->isDigit() && !currB->isPunct() && !currB->isSpace()) {
++currB;
}
// compare these sequences
const QStringRef& subA(a.midRef(begSeqA - a.unicode(), currA - begSeqA));
const QStringRef& subB(b.midRef(begSeqB - b.unicode(), currB - begSeqB));
const int cmp = QStringRef::localeAwareCompare(subA, subB);
if (cmp != 0) {
return cmp < 0 ? -1 : +1;
}
if (currA->isNull() || currB->isNull()) {
break;
}
// find sequence of characters ending at the first non-character
while (currA->isPunct() || currA->isSpace() || currB->isPunct() || currB->isSpace()) {
if (*currA != *currB) {
return (*currA < *currB) ? -1 : +1;
}
++currA;
++currB;
}
// now some digits follow...
if ((*currA == '0') || (*currB == '0')) {
// one digit-sequence starts with 0 -> assume we are in a fraction part
// do left aligned comparison (numbers are considered left aligned)
while (1) {
if (!currA->isDigit() && !currB->isDigit()) {
break;
} else if (!currA->isDigit()) {
return +1;
} else if (!currB->isDigit()) {
return -1;
} else if (*currA < *currB) {
return -1;
} else if (*currA > *currB) {
return + 1;
}
++currA;
++currB;
}
} else {
// No digit-sequence starts with 0 -> assume we are looking at some integer
// do right aligned comparison.
//
// The longest run of digits wins. That aside, the greatest
// value wins, but we can't know that it will until we've scanned
// both numbers to know that they have the same magnitude.
bool isFirstRun = true;
int weight = 0;
while (1) {
if (!currA->isDigit() && !currB->isDigit()) {
if (weight != 0) {
return weight;
}
break;
} else if (!currA->isDigit()) {
if (isFirstRun) {
return *currA < *currB ? -1 : +1;
} else {
return -1;
}
} else if (!currB->isDigit()) {
if (isFirstRun) {
return *currA < *currB ? -1 : +1;
} else {
return +1;
}
} else if ((*currA < *currB) && (weight == 0)) {
weight = -1;
} else if ((*currA > *currB) && (weight == 0)) {
weight = + 1;
}
++currA;
++currB;
isFirstRun = false;
}
}
begSeqA = currA;
begSeqB = currB;
}
if (currA->isNull() && currB->isNull()) {
return 0;
}
return currA->isNull() ? -1 : + 1;
}
*/
static inline QChar getNextChar(const QString &s, int location)
{
return (location < s.length()) ? s.at(location) : QChar();
}
int naturalCompare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs)
{
for (int l1 = 0, l2 = 0; l1 <= s1.count() && l2 <= s2.count(); ++l1, ++l2) {
// skip spaces, tabs and 0's
QChar c1 = getNextChar(s1, l1);
while (c1.isSpace())
c1 = getNextChar(s1, ++l1);
QChar c2 = getNextChar(s2, l2);
while (c2.isSpace())
c2 = getNextChar(s2, ++l2);
if (c1.isDigit() && c2.isDigit()) {
while (c1.digitValue() == 0)
c1 = getNextChar(s1, ++l1);
while (c2.digitValue() == 0)
c2 = getNextChar(s2, ++l2);
int lookAheadLocation1 = l1;
int lookAheadLocation2 = l2;
int currentReturnValue = 0;
// find the last digit, setting currentReturnValue as we go if it isn't equal
for (
QChar lookAhead1 = c1, lookAhead2 = c2;
(lookAheadLocation1 <= s1.length() && lookAheadLocation2 <= s2.length());
lookAhead1 = getNextChar(s1, ++lookAheadLocation1),
lookAhead2 = getNextChar(s2, ++lookAheadLocation2)
) {
bool is1ADigit = !lookAhead1.isNull() && lookAhead1.isDigit();
bool is2ADigit = !lookAhead2.isNull() && lookAhead2.isDigit();
if (!is1ADigit && !is2ADigit)
break;
if (!is1ADigit)
return -1;
if (!is2ADigit)
return 1;
if (currentReturnValue == 0) {
if (lookAhead1 < lookAhead2) {
currentReturnValue = -1;
} else if (lookAhead1 > lookAhead2) {
currentReturnValue = 1;
}
}
}
if (currentReturnValue != 0)
return currentReturnValue;
}
if (cs == Qt::CaseInsensitive) {
if (!c1.isLower()) c1 = c1.toLower();
if (!c2.isLower()) c2 = c2.toLower();
}
int r = QString::localeAwareCompare(c1, c2);
if (r < 0)
return -1;
if (r > 0)
return 1;
}
// The two strings are the same (02 == 2) so fall back to the normal sort
return QString::compare(s1, s2, cs);
QCollator c;
c.setCaseSensitivity(caseSensitivity);
c.setNumericMode(true);
return c.compare(s1, s2);
}
bool naturalSortLessThanCS( const QString &left, const QString &right )
{

View File

@ -67,6 +67,10 @@
#define SIDEBAR_SPLITTER_STATUS "SIDEBAR_SPLITTER_STATUS"
#define COMICS_GRID_COVER_SIZES "COMICS_GRID_COVER_SIZES"
#define USE_BACKGROUND_IMAGE_IN_GRID_VIEW "USE_BACKGROUND_IMAGE_IN_GRID_VIEW"
#define OPACITY_BACKGROUND_IMAGE_IN_GRID_VIEW "OPACITY_BACKGROUND_IMAGE_IN_GRID_VIEW"
#define BLUR_RADIUS_BACKGROUND_IMAGE_IN_GRID_VIEW "BLUR_RADIUS_BACKGROUND_IMAGE_IN_GRID_VIEW"
#define NUM_DAYS_BETWEEN_VERSION_CHECKS "NUM_DAYS_BETWEEN_VERSION_CHECKS"
#define LAST_VERSION_CHECK "LAST_VERSION_CHECK"