Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions fuzzy.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ nominativeHours = час\\два\\три\\четыре\\пять\\шесть\\с
minuteRefer = 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8 9 9 9 9 9 10 10 10 10 10 11 11 11 11 11 12 12
minutes = Ровно %0\\Пять минут %1\\Десять минут %1\\Четверть %1\\Двадцать минут %1\\Двадцать пять минут %1\\Половина %1\\Без двадцати пяти %0\\Без двадцати %0\\Без четверти %0\\Без десяти %0\\Без пяти %0\\Почти %0
genitiveHours = первого\\второго\\третьего\\четвертого\\пятого\\шестого\\седьмого\\восьмого\\девятого\\десятого\\одиннадцатого\\двенадцатого

[dayparts]
names = Ночь\\Раннее утро\\Утро\\Почти полдень\\Полдень\\День\\Вечер\\Поздний вечер
hourRefer = 0 0 0 0 0 1 1 1 2 2 2 3 4 5 5 5 5 5 6 6 6 6 7 7

[weekparts]
names = Начало недели\\Середина недели\\Конец недели\\Выходные
dayRefer = 0 1 1 1 2 3 3
39 changes: 31 additions & 8 deletions fuzzy_clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,43 @@

#include "fuzzy_clock.h"

bool fuzzyClock::isValid() const
namespace {

// каждая ссылка должна указывать на существующий элемент списка
bool refersInside(const std::vector<int> &refer, size_t referSize, const std::vector<QString> &items)
{
return refer.size() == referSize
&& std::all_of(refer.begin(), refer.end(), [&items](int i) { return i >= 0 && i < int(items.size()); });
}

}

bool fuzzyClock::isValid(Fuzziness fuzziness) const
{
if (vectMinuteRefer.size() != 60 || vectNominativeHours.size() != 12 || vectGenitiveHours.size() != 12)
return false;
// каждая ссылка из minuteRefer должна указывать на существующую фразу
return std::all_of(vectMinuteRefer.begin(), vectMinuteRefer.end(),
[this](int i) { return i >= 0 && i < int(vectMinutes.size()); });
switch (fuzziness) {
case Fuzziness::DayPart:
return refersInside(vectHourRefer, 24, vectDayParts);
case Fuzziness::WeekPart:
return refersInside(vectDayRefer, 7, vectWeekParts);
case Fuzziness::Minutes:
break;
}
return vectNominativeHours.size() == 12 && vectGenitiveHours.size() == 12
&& refersInside(vectMinuteRefer, 60, vectMinutes);
}

QString fuzzyClock::text(const QTime &time) const
QString fuzzyClock::text(const QDateTime &dateTime, Fuzziness fuzziness) const
{
if (!isValid())
if (!isValid(fuzziness))
return QStringLiteral("Ошибка в fuzzy.conf");

const QTime time = dateTime.time();

if (fuzziness == Fuzziness::DayPart)
return vectDayParts[vectHourRefer[time.hour()]];
if (fuzziness == Fuzziness::WeekPart)
return vectWeekParts[vectDayRefer[dateTime.date().dayOfWeek() - 1]]; // dayOfWeek(): 1 - пн, 7 - вс

const int minute = time.minute();
int hour = time.hour() % 12; // 0..11

Expand Down
17 changes: 14 additions & 3 deletions fuzzy_clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@
#define FUZZY_CLOCK_H

#include <vector>
#include <QDateTime>
#include <QString>
#include <QTime>

// Преобразование времени в «неточную» фразу. Данные загружаются из fuzzy.conf
struct fuzzyClock
{
enum class Fuzziness {
Minutes, // "Четверть пятого" - с точностью до пяти минут, секция [oldstyle]
DayPart, // "Вечер" - часть суток, секция [dayparts]
WeekPart // "Середина недели" - часть недели, секция [weekparts]
};

std::vector<QString> vectMinutes;
std::vector<int> vectMinuteRefer; // для каждой минуты - номер фразы в vectMinutes
std::vector<QString> vectNominativeHours;
std::vector<QString> vectGenitiveHours;

bool isValid() const;
QString text(const QTime &time) const;
std::vector<QString> vectDayParts;
std::vector<int> vectHourRefer; // для каждого часа (0..23) - номер в vectDayParts
std::vector<QString> vectWeekParts;
std::vector<int> vectDayRefer; // для каждого дня недели (пн..вс) - номер в vectWeekParts

bool isValid(Fuzziness fuzziness) const;
QString text(const QDateTime &dateTime, Fuzziness fuzziness) const;
};

#endif // FUZZY_CLOCK_H
4 changes: 3 additions & 1 deletion fuzzy_clock.pro
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ QT += core gui \

LIBS += \
-lwtsapi32 \
-luser32
-luser32 \
-lgdi32 \
-lcomdlg32

RC_ICONS = app.ico
ICON = app.ico
Expand Down
Loading