Skip to content

Commit

Permalink
FIXed multiRow/MultiColumn distribution algorithm in JKQTPKey
Browse files Browse the repository at this point in the history
  • Loading branch information
jkriege2 committed Jan 9, 2024
1 parent 0423205 commit e0df1a8
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 32 deletions.
6 changes: 3 additions & 3 deletions doc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,12 @@ if(JKQtPlotter_BUILD_EXAMPLES)
functionplot/functionplot,functionplot_fy
styledboxplot/test_styledboxplot
multiplot/multiplot,multiplot_controlwindow
#symbols_and_styles
symbols_and_styles
symbols_and_errors
stepplots
stackedbars/stackedbars,stackedbars_hor
#geo_arrows
#geo_simple
geo_arrows
geo_simple
geometric
imageplot/imageplot,imageplot__scale02,imageplot__smallscalelimitcolor,imageplot__smallscalecolor,imageplot__smallscaletransparent/--iteratefunctorsteps
imageplot_modifier
Expand Down
Binary file modified doc/images/keylayouts/JKQTPKeyLayout_multi_row.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/keylayouts/JKQTPKeyLayout_outsideright_multi_column.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/keylayouts/JKQTPKeyLayout_outsideright_multi_row.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
109 changes: 82 additions & 27 deletions lib/jkqtplotter/jkqtpkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,37 +225,71 @@ void JKQTPBaseKey::modifySize(JKQTPEnhancedPainter &painter, KeySizeDescription
#ifdef JKQTBP_AUTOTIMER
JKQTPAutoOutputTimer jkaat(QString("JKQTPBaseKey[%1]::modifySize()").arg(objectName()));
#endif
// in odd cases (many plots), the initial key size may be larger than the actual plot. then we can have negative sizes.
// in these cases we correct them preliminary plot size to 80% of the widget size (=available size)!
const auto widgetSize=QSizeF(parent->getWidth(), parent->getHeight());
if (preliminaryPlotSize.width()<0) preliminaryPlotSize.setWidth(widgetSize.width()*0.8);
if (preliminaryPlotSize.height()<0) preliminaryPlotSize.setHeight(widgetSize.height()*0.8);


const auto lay=getLayout();
if (lay==JKQTPKeyLayoutMultiColumn || lay==JKQTPKeyLayoutMultiRow) {
std::function<bool(QSizeF, QSizeF)> fCompare=[](const QSizeF& requiredSize, const QSizeF& preliminaryPlotSize) {
return true;
};

std::function<bool(QSizeF, QSizeF)> fcmpSizeTooLarge=[](const QSizeF& requiredSize, const QSizeF& preliminaryPlotSize) { return true; };

bool increaseColumnCount=true;
bool fillMaxMode=false;
if (currentKeyLayout.keyLocation==KeySizeDescription::keyInside) {
fCompare=[](const QSizeF& requiredSize, const QSizeF& preliminaryPlotSize) {
fcmpSizeTooLarge=[](const QSizeF& requiredSize, const QSizeF& preliminaryPlotSize) {
return (requiredSize.width()>preliminaryPlotSize.width() || requiredSize.height()>preliminaryPlotSize.height());
};
};
} else if (currentKeyLayout.keyLocation==KeySizeDescription::keyOutsideTop || currentKeyLayout.keyLocation==KeySizeDescription::keyOutsideBottom) {
fCompare=[](const QSizeF& requiredSize, const QSizeF& preliminaryPlotSize) {
return (requiredSize.height()>preliminaryPlotSize.height());
};
fcmpSizeTooLarge=[widgetSize](const QSizeF& requiredSize, const QSizeF& preliminaryPlotSize) {
return (requiredSize.width()>preliminaryPlotSize.width());
};
increaseColumnCount=true;
fillMaxMode=true;

} else if (currentKeyLayout.keyLocation==KeySizeDescription::keyOutsideLeft || currentKeyLayout.keyLocation==KeySizeDescription::keyOutsideRight) {
fCompare=[](const QSizeF& requiredSize, const QSizeF& preliminaryPlotSize) {
return (requiredSize.width()>preliminaryPlotSize.width());
};
fcmpSizeTooLarge=[widgetSize](const QSizeF& requiredSize, const QSizeF& preliminaryPlotSize) {
return (requiredSize.height()>preliminaryPlotSize.height());
};
increaseColumnCount=false;
fillMaxMode=true;
}

const int itemCnt=currentKeyLayout.d->countItems();
int newCount=1;
while ((newCount<=itemCnt) && (currentKeyLayout.requiredSize.width()>preliminaryPlotSize.width() || currentKeyLayout.requiredSize.height()>preliminaryPlotSize.height())) {
newCount++;
if (lay==JKQTPKeyLayoutMultiColumn) {
currentKeyLayout.d->redistributeOverColumns(newCount);
} else if (lay==JKQTPKeyLayoutMultiRow) {
currentKeyLayout.d->redistributeOverRows(newCount);
const auto initialLayout=currentKeyLayout;
if (fillMaxMode) {
int newCount=itemCnt+1;
bool notSizeOK=true;
while (newCount>1 && notSizeOK) {
newCount--; // increase number of rows/columns
currentKeyLayout=initialLayout; // reset to initial layout, which should have one column only!
// this is required, so redistribute...() does not scramble the order
if (increaseColumnCount) {
currentKeyLayout.d->redistributeOverColumns(newCount, lay==JKQTPKeyLayoutMultiColumn);
} else {
currentKeyLayout.d->redistributeOverRows(newCount, lay==JKQTPKeyLayoutMultiRow);
}
calcLayoutSize(painter, currentKeyLayout);
notSizeOK=fcmpSizeTooLarge(currentKeyLayout.requiredSize, preliminaryPlotSize);
}
} else {
int newCount=1;
while (newCount<itemCnt && fcmpSizeTooLarge(currentKeyLayout.requiredSize, preliminaryPlotSize)) {
newCount++; // increase number of rows/columns
currentKeyLayout=initialLayout; // reset to initial layout, which should have one column only!
// this is required, so redistribute...() does not scramble the order
if (increaseColumnCount) {
currentKeyLayout.d->redistributeOverColumns(newCount, lay==JKQTPKeyLayoutMultiColumn);
} else {
currentKeyLayout.d->redistributeOverRows(newCount, lay==JKQTPKeyLayoutMultiRow);
}
calcLayoutSize(painter, currentKeyLayout);
}
calcLayoutSize(painter, currentKeyLayout);
}

}
}

Expand Down Expand Up @@ -380,6 +414,7 @@ JKQTPBaseKey::KeySizeDescription &JKQTPBaseKey::KeySizeDescription::operator=(co
return *this;
}


JKQTPBaseKey::KeyColumnDescription::KeyColumnDescription():
rows()
{
Expand Down Expand Up @@ -470,12 +505,12 @@ void JKQTPBaseKey::KeyLayoutDescription::redistributeIntoOneColumn()
}
}

void JKQTPBaseKey::KeyLayoutDescription::redistributeOverRows(int rowCnt)
void JKQTPBaseKey::KeyLayoutDescription::redistributeOverRows(int rowCnt, bool rowMajor)
{
const int itemCnt=countItems();
if (itemCnt>1) {
const int colCnt=static_cast<int>(ceil(static_cast<float>(itemCnt)/static_cast<float>(rowCnt)));
if (colCnt>1) {
if (true) {
redistributeIntoOneColumn();
const auto items=columns[0].rows;
columns.clear();
Expand All @@ -484,17 +519,26 @@ void JKQTPBaseKey::KeyLayoutDescription::redistributeOverRows(int rowCnt)
for (int c=0; c<colCnt; c++) {
columns.push_back(KeyColumnDescription());
}
for (int r=0; r<rowCnt; r++) {
if (rowMajor) {
for (int r=0; r<rowCnt; r++) {
for (int c=0; c<colCnt; c++) {
if (i<itemCnt) columns[c].rows.append(items[i]);
i++;
}
}
} else {
for (int c=0; c<colCnt; c++) {
if (i<itemCnt) columns[c].rows.append(items[i]);
i++;
for (int r=0; r<rowCnt; r++) {
if (i<itemCnt) columns[c].rows.append(items[i]);
i++;
}
}
}
}
}
}

void JKQTPBaseKey::KeyLayoutDescription::redistributeOverColumns(int colCnt)
void JKQTPBaseKey::KeyLayoutDescription::redistributeOverColumns(int colCnt, bool colMajor)
{
const int itemCnt=countItems();
if (itemCnt>1) {
Expand All @@ -506,9 +550,20 @@ void JKQTPBaseKey::KeyLayoutDescription::redistributeOverColumns(int colCnt)
int i=0;
for (int c=0; c<colCnt; c++) {
columns.push_back(KeyColumnDescription());
}
if (colMajor) {
for (int c=0; c<colCnt; c++) {
for (int r=0; r<rowCnt; r++) {
if (i<itemCnt) columns[c].rows.append(items[i]);
i++;
}
}
} else {
for (int r=0; r<rowCnt; r++) {
if (i<itemCnt) columns[c].rows.append(items[i]);
i++;
for (int c=0; c<colCnt; c++) {
if (i<itemCnt) columns[c].rows.append(items[i]);
i++;
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/jkqtplotter/jkqtpkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ class JKQTPLOTTER_LIB_EXPORT JKQTPBaseKey: public QObject {
/** \brief put all items into one column */
void redistributeIntoOneColumn();
/** \brief takes all elements in columns and redistributes them over the given number of rows, items are distributed as equally as possible (last row may have fewer items) */
void redistributeOverRows(int rows);
void redistributeOverRows(int rows, bool rowMajor=true);
/** \brief takes all elements in columns and redistributes them over the given number of columns, items are distributed as equally as possible (last column may have fewer items) */
void redistributeOverColumns(int cols);
void redistributeOverColumns(int cols, bool colMajor=true);
};

/** \brief calculates all layout properties of the key/legend,necessary to size and draw it
Expand Down
Binary file modified screenshots/datastore_regression_linweight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/errorbarstyles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/geo_arrows.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/geo_arrows_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/geo_simple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/geo_simple_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/geometric.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/geometric_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/mandelbrot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/mandelbrot_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/symbols_and_styles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/symbols_and_styles_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e0df1a8

Please sign in to comment.