Proof of concept for improving the swap function in the Align & Distribute tool in Scribus
- Much shorter code
- Probably more correct
- CycleLeft does not exactly the same as the current SwapLeft, but should be close enough and much simpler.
- Reverse is a "real" swap.
Warning: the initial list is on purpose out of order (in Scribus AObjects
is on the order of selection!).
The items are inserted as c a b
, and when ordered they are indeed a b c
.
The order (of selection) is kept in AObjects
and you have to keep an eye on the coordinates to figure out the current order.
Links:
- The original code: https://github.com/scribusproject/scribus/blob/master/scribus/scribusdoc.cpp#L13192
- The Scribus ticket: https://bugs.scribus.net/view.php?id=15205
QMap
s are "automatically" sorted.
I tried to use a QPoint
as the key and a reference to the list's item as the value, but I could not find a way to set the value of the item in a way, that the one in the list also gets changed.
Finally, I've switched to the QList
based solution in the main.cpp
file.
#include <QMultiMap>
#include <QPoint>
bool operator <(QPoint a, QPoint b)
{
return a.x() < b.x() || (a.x() == b.x() && a.y() < b.y());
}
int main()
{
// [...]
QMultiMap<QPoint, AObject*> def{};
for (auto &a: aObjects) {
def.insert({a.x, a.y}, &a);
}
QMutableMapIterator<QPoint, AObject*> it(def);
it.toFront();
it.next();
auto swapX = it.value()->x;
auto prevIt = it;
it.next();
while (it.hasNext()) {
auto& temp = prevIt.value();
temp->y = 20; // it does not change the items in the original list
auto prevIt = it;
it.next();
}
// [...]
}
int main()
{
// [...]
auto it = def.begin();
auto swapX = it.value()->x;
auto swapY = it.value()->y;
auto prevIt = it;
++it;
while (it != def.end()) {
prevIt.value()->y = 20; // it does not change the items in the original list
qDebug() << prevIt.key();
qDebug() << prevIt.value()->x;
auto prevIt = it;
++it;
}
// [...]
}
- the current state
- groking
swapLeft
andswapRight
- cf. the README-swap-vertically
- http://www.mergely.com/q71dIT9I/ (it's nicer on https://www.diffchecker.com/diff)
- groking
- the new implementation
- issues with map
- we need qmultimap
- how to write to the reference in the qmultimap?
- simpler with qlist
- issues with map
- algorithms?
iter_swap
std::iter_swap(it1, it2)
is equivalent tostd::swap(*it1, *it2)
reverse