Skip to content

Instantly share code, notes, and snippets.

@aoloe
Last active March 21, 2018 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aoloe/1b928acf6356576209196b30f90c2c03 to your computer and use it in GitHub Desktop.
Save aoloe/1b928acf6356576209196b30f90c2c03 to your computer and use it in GitHub Desktop.
Refactoring AObjects

Refactoring AObjects

Goals

  • rename to AlignObjects and AlignObject
  • check if AlignObject.Objects ever contains a different number of PageItem* than one.

Facts

  • AlignObjs is defined in scribusstructs.h and contains:

    struct AlignObjs
    {
        int ObjNr;
        int Group;
        double x1;
        double y1;
        double x2;
        double y2;
        double width;
        double height;
        QList<PageItem*> Objects;
    };
  • AObjects is defined in scribusdoc.h as a QList<AlignObjs> AObjects

Assumptions

  • The AObjects list is only filled in ScribusDoc::buildAlignItemList()
  • There is another AObjects in the PdfLibCore but it's not the same structure.

Trimming down buildAlignItemList

... in my eyes: no way to have more (or less) than one item in AObjects.Objects.

See src/main.cpp

Refactoring

I propose the following changes:

  • Directly store the PageItem in AlignObjects (not in an Objects list)
  • use small case names for variables.

See src/main-refactored.cpp

CMAKE_MINIMUM_REQUIRED(VERSION 3.4)
PROJECT(align-objects)
SET(CMAKE_CXX_STANDARD 14)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt5Core)
add_executable(align-objects
main.cpp
)
target_link_libraries(align-objects
Qt5::Core
)
add_executable(align-objects-refactored
main-refactored.cpp
)
target_link_libraries(align-objects-refactored
Qt5::Core
)
#include <QDebug>
#include <QList>
class PageItem
{
public:
char t{};
};
using SelectionList = QList<PageItem>;
struct AlignObject
{
// AlignObject(PageItem* pageItem, double x1, ... double height) : pageItem{pageItem}, ... : {} // eventually cache x1, ... height
// double x1, y1, x2, y2 -> should they really be cached?
// double width, height -> should they really be cached?
PageItem pageItem;
};
class Selection
{
public:
int count() {return selectionList.count();}
PageItem itemAt(int i) {return selectionList.at(i);}
private:
SelectionList selectionList{{'a'}, {'b'}, {'c'}};
};
class ScribusDoc
{
public:
void buildAlignItemList()
{
alignObjects.clear();
for (int i = 0; i < selection.count(); i++) {
auto currItem = selection.itemAt(i);
AlignObject pageItem{currItem};
alignObjects.append(pageItem);
}
}
void render()
{
qDebug() << "-----";
for (auto a: alignObjects) {
qDebug() << a.pageItem.t;
}
}
private:
Selection selection;
QList<AlignObject> alignObjects{};
};
int main()
{
ScribusDoc doc;
doc.buildAlignItemList();
doc.render();
}
#include <QDebug>
#include <QList>
class PageItem
{
public:
char t{};
};
using SelectionList = QList<PageItem>;
struct AlignObjs
{
// ObjNr -> cannot be grepped in the whole scribus source
// Group -> not in use in scribusdoc.cpp. hard to say if it is elsewhere
// double x1, y1, x2, y2 -> should they really be cached?
// double width, height -> should they really be cached?
QList<PageItem> Objects;
};
class Selection
{
public:
int count() {return selectionList.count();}
PageItem itemAt(int i) {return selectionList.at(i);}
private:
SelectionList selectionList{{'a'}, {'b'}, {'c'}};
};
class ScribusDoc
{
public:
void buildAlignItemList()
{
struct AlignObjs Object;
AObjects.clear();
for (int i = 0; i < selection.count(); i++) {
auto currItem = selection.itemAt(i);
Object.Objects.clear();
// cache x1, ... height
Object.Objects.append(currItem);
AObjects.append(Object);
}
}
void render()
{
qDebug() << "-----";
for (auto a: AObjects) {
qDebug() << a.Objects.count();
}
}
private:
Selection selection;
QList<AlignObjs> AObjects{};
};
int main()
{
ScribusDoc doc;
doc.buildAlignItemList();
doc.render();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment