leocad/common/lc_category.cpp

153 lines
4.2 KiB
C++
Raw Normal View History

2013-08-09 06:57:18 +02:00
#include "lc_global.h"
#include "lc_category.h"
#include "lc_file.h"
#include "lc_profile.h"
2013-08-16 01:43:18 +02:00
lcArray<lcLibraryCategory> gCategories;
2013-08-09 06:57:18 +02:00
void lcResetDefaultCategories()
{
lcResetCategories(gCategories);
lcRemoveProfileKey(LC_PROFILE_CATEGORIES);
}
void lcLoadDefaultCategories(bool BuiltInLibrary)
{
QByteArray Buffer = lcGetProfileBuffer(LC_PROFILE_CATEGORIES);
2013-08-09 06:57:18 +02:00
if (Buffer.isEmpty() || !lcLoadCategories(Buffer, gCategories))
2013-08-09 06:57:18 +02:00
lcResetCategories(gCategories, BuiltInLibrary);
}
void lcSaveDefaultCategories()
{
2016-02-17 00:11:52 +01:00
QByteArray ByteArray;
QTextStream Stream(&ByteArray, QIODevice::WriteOnly);
2013-08-09 06:57:18 +02:00
2016-02-17 00:11:52 +01:00
lcSaveCategories(Stream, gCategories);
2013-08-09 06:57:18 +02:00
2016-02-17 00:11:52 +01:00
lcSetProfileBuffer(LC_PROFILE_CATEGORIES, ByteArray);
2013-08-09 06:57:18 +02:00
}
2013-08-16 01:43:18 +02:00
void lcResetCategories(lcArray<lcLibraryCategory>& Categories, bool BuiltInLibrary)
2013-08-09 06:57:18 +02:00
{
const char DefaultCategories[] =
{
"Animal=^%Animal | ^%Bone\n"
"Antenna=^%Antenna\n"
"Arch=^%Arch\n"
"Bar=^%Bar\n"
"Baseplate=^%Baseplate | ^%Platform\n"
"Boat=^%Boat\n"
"Brick=^%Brick\n"
"Container=^%Container | ^%Box | ^Chest | ^%Storage | ^Mailbox\n"
"Door and Window=^%Door | ^%Window | ^%Glass | ^%Freestyle | ^%Gate | ^%Garage | ^%Roller\n"
"Electric=^%Electric\n"
"Hinge and Bracket=^%Hinge | ^%Bracket | ^%Turntable\n"
"Hose=^%Hose | ^%String\n"
"Minifig=^%Minifig\n"
"Miscellaneous=^%Arm | ^%Barrel | ^%Brush | ^%Claw | ^%Cockpit | ^%Conveyor | ^%Crane | ^%Cupboard | ^%Fence | ^%Jack | ^%Ladder | ^%Motor | ^%Rock | ^%Rope | ^%Sheet | ^%Sports | ^%Staircase | ^%Stretcher | ^%Tap | ^%Tipper | ^%Trailer | ^%Umbrella | ^%Winch\n"
"Other=^%Ball | ^%Belville | ^%Die | ^%Duplo | ^%Fabuland | ^%Figure | ^%Homemaker | ^%Maxifig | ^%Microfig | ^%Mursten | ^%Scala | ^%Znap\n"
"Panel=^%Panel | ^%Castle Wall | ^%Castle Turret\n"
"Plant=^%Plant\n"
"Plate=^%Plate\n"
"Round=^%Cylinder | ^%Cone | ^%Dish | ^%Dome | ^%Hemisphere | ^%Round\n"
"Sign and Flag=^%Flag | ^%Roadsign | ^%Streetlight | ^%Flagpost | ^%Lamppost | ^%Signpost\n"
"Slope=^%Slope | ^%Roof\n"
"Space=^%Space\n"
"Sticker=^%Sticker\n"
"Support=^%Support\n"
"Technic=^%Technic | ^%Rack\n"
"Tile=^%Tile\n"
"Train=^%Train | ^%Monorail | ^%Magnet\n"
"Tyre and Wheel=^%Tyre | %^Wheel | %^Wheels | ^%Castle Wagon\n"
"Vehicle=^%Bike | ^%Canvas | ^%Car | ^%Excavator | ^%Exhaust | ^%Forklift | ^%Grab Jaw | ^%Landing | ^%Motorcycle | ^%Plane | ^%Propellor | ^%Tail | ^%Tractor | ^%Vehicle | ^%Wheelbarrow\n"
"Windscreen=^%Windscreen\n"
"Wedge=^%Wedge\n"
"Wing=^%Wing\n"
};
const char BuiltInCategories[] =
{
2014-09-10 03:41:37 +02:00
"Baseplate=^%Baseplate\n"
2013-08-09 06:57:18 +02:00
"Brick=^%Brick\n"
"Plate=^%Plate\n"
2014-09-11 21:55:34 +02:00
"Slope=^%Slope\n"
"Tile=^%Tile\n"
2013-08-09 06:57:18 +02:00
};
QByteArray Buffer;
2013-08-09 06:57:18 +02:00
if (BuiltInLibrary)
Buffer.append(BuiltInCategories, sizeof(BuiltInCategories));
2013-08-09 06:57:18 +02:00
else
Buffer.append(DefaultCategories, sizeof(DefaultCategories));
2013-08-09 06:57:18 +02:00
lcLoadCategories(Buffer, Categories);
2013-08-09 06:57:18 +02:00
}
bool lcLoadCategories(const QString& FileName, lcArray<lcLibraryCategory>& Categories)
2013-08-09 06:57:18 +02:00
{
QFile File(FileName);
2013-08-09 06:57:18 +02:00
if (!File.open(QIODevice::ReadOnly))
2013-08-09 06:57:18 +02:00
return false;
QByteArray FileData = File.readAll();
return lcLoadCategories(FileData, Categories);
2013-08-09 06:57:18 +02:00
}
bool lcLoadCategories(const QByteArray& Buffer, lcArray<lcLibraryCategory>& Categories)
2013-08-09 06:57:18 +02:00
{
Categories.RemoveAll();
QTextStream Stream(Buffer);
2013-08-09 06:57:18 +02:00
for (QString Line = Stream.readLine(); !Line.isNull(); Line = Stream.readLine())
2013-08-09 06:57:18 +02:00
{
int Equals = Line.indexOf('=');
2013-08-09 06:57:18 +02:00
if (Equals == -1)
2013-08-09 06:57:18 +02:00
continue;
QString Name = Line.left(Equals);
QString Keywords = Line.mid(Equals + 1);
2013-08-09 06:57:18 +02:00
lcLibraryCategory& Category = Categories.Add();
2016-05-02 20:22:38 +02:00
Category.Name = Name.toLatin1().constData();
Category.Keywords = Keywords.toLatin1().constData();
2013-08-09 06:57:18 +02:00
}
return true;
}
bool lcSaveCategories(const QString& FileName, const lcArray<lcLibraryCategory>& Categories)
2013-08-09 06:57:18 +02:00
{
2016-02-17 00:11:52 +01:00
QFile File(FileName);
2013-08-09 06:57:18 +02:00
2016-02-17 00:11:52 +01:00
if (!File.open(QIODevice::WriteOnly))
2013-08-09 06:57:18 +02:00
return false;
2016-02-17 00:11:52 +01:00
QTextStream Stream(&File);
return lcSaveCategories(Stream, Categories);
2013-08-09 06:57:18 +02:00
}
2016-02-17 00:11:52 +01:00
bool lcSaveCategories(QTextStream& Stream, const lcArray<lcLibraryCategory>& Categories)
2013-08-09 06:57:18 +02:00
{
2016-02-17 00:11:52 +01:00
QString Format("%1=%2\r\n");
2013-08-09 06:57:18 +02:00
for (int CategoryIdx = 0; CategoryIdx < Categories.GetSize(); CategoryIdx++)
{
const lcLibraryCategory& Category = Categories[CategoryIdx];
2016-02-17 00:11:52 +01:00
Stream << Format.arg((const char*)Category.Name, (const char*)Category.Keywords);
2013-08-09 06:57:18 +02:00
}
2016-02-17 00:11:52 +01:00
Stream.flush();
2013-08-09 06:57:18 +02:00
return true;
}