From e94dd9073c3c304fcaba8db30ab2c7f3f44ca942 Mon Sep 17 00:00:00 2001 From: Mykola Dimura Date: Sun, 29 May 2016 16:28:25 +0200 Subject: [PATCH] Fix map rendering to be dpi-agnostic --- Galaxy.cpp | 14 +++++++++----- Galaxy.h | 2 +- MainWindow.cpp | 21 ++++++++++++--------- MainWindow.h | 8 ++++---- main.cpp | 2 ++ 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/Galaxy.cpp b/Galaxy.cpp index 4df65f5..161bb0c 100644 --- a/Galaxy.cpp +++ b/Galaxy.cpp @@ -526,15 +526,18 @@ QString Galaxy::blackHoleNextLootChange(unsigned row) const return changes; } -QImage Galaxy::map(float scale) const +QImage Galaxy::map(const unsigned width) const { - QImage image((mapRect.width()+8)*scale,(mapRect.height()+6)*scale,QImage::Format_ARGB32); + //QImage image((mapRect.width()+8)*scale,(mapRect.height()+6)*scale,QImage::Format_ARGB32); + unsigned height=width*mapRect.height()/mapRect.width(); + QImage image(width,height,QImage::Format_ARGB32); image.fill(Qt::black); QPainter p(&image); p.setPen(QPen(QColor(Qt::white))); p.setBrush(QBrush(QColor(Qt::white),Qt::SolidPattern)); p.setRenderHint(QPainter::Antialiasing, true); - QFont font("Verdana",int(scale*1.2/qGuiApp->devicePixelRatio())); + QFont font("Verdana"); + font.setPixelSize(height*0.020); p.setFont(font); //prepare base names QMap starIdToBases; @@ -567,11 +570,12 @@ QImage Galaxy::map(float scale) const planetsStr+=planetStr.arg(size).arg(economy).arg(color); } //Draw stars + double scale=double(width*0.92)/mapRect.width(); //QMap starIdToPos; for(const auto& pair:starMap) { const Star& star=pair.second; - QPointF pos=star.position()-mapRect.topLeft()+QPointF(4,3); + QPointF pos=star.position()-mapRect.topLeft()+QPointF(6,4); pos*=scale; //starIdtoPos[star.id()]=pos; QString owner=star.owner(); @@ -596,7 +600,7 @@ QImage Galaxy::map(float scale) const QStaticText planetsText(starIdToPlanets.value(star.id())); planetsText.setTextFormat(Qt::RichText); planetsText.prepare(QTransform(),font); - QPointF panetsPos=pos+QPointF(-planetsText.size().width()*0.5,-scale*3.0); + QPointF panetsPos=pos+QPointF(-planetsText.size().width()*0.5,-scale*3.3); p.drawStaticText(panetsPos,planetsText); } return image; diff --git a/Galaxy.h b/Galaxy.h index 4aff3ad..e584019 100644 --- a/Galaxy.h +++ b/Galaxy.h @@ -108,7 +108,7 @@ class Galaxy { return _minSellPrice; } - QImage map(float scale=10) const; + QImage map(const unsigned width=10) const; private: unsigned marketStarId(unsigned row) const; unsigned equipmentStarId(unsigned row) const; diff --git a/MainWindow.cpp b/MainWindow.cpp index 8dc800b..6363f67 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -151,11 +151,13 @@ MainWindow::MainWindow(QWidget *parent) : reportButton->setMenu(&saveReportMenu); reportButton->setPopupMode(QToolButton::MenuButtonPopup); - _mapScaleSpinBox.setPrefix("x"); - _mapScaleSpinBox.setToolTip(tr("Map scale")); - _mapScaleSpinBox.setWhatsThis(tr("Map scale")); + //_mapScaleSpinBox.setPrefix("x"); + _mapScaleSpinBox.setMaximum(10000); + _mapScaleSpinBox.setSingleStep(50); + _mapScaleSpinBox.setToolTip(tr("Map width")); + _mapScaleSpinBox.setWhatsThis(tr("Map width")); ui->mainToolBar->insertWidget(ui->mainToolBar->actions()[3],&_mapScaleSpinBox); - connect(&_mapScaleSpinBox,SIGNAL(valueChanged(double)),this,SLOT(setMapScale(double))); + connect(&_mapScaleSpinBox,SIGNAL(valueChanged(int)),this,SLOT(setMapScale(int))); sound.setSource(QUrl::fromLocalFile("Click1.wav")); sound.setVolume(1.0); @@ -297,8 +299,8 @@ void MainWindow::readSettings() shortSleep=settings.value("shortSleep",25).toInt(); maxGenerationTime=settings.value("maxGenerationTime",120000).toInt(); - mapScale=settings.value("mapScale",7.f).toFloat(); - _mapScaleSpinBox.setValue(mapScale); + mapWidth=std::max(10,settings.value("mapWidth",800).toInt()); + _mapScaleSpinBox.setValue(mapWidth); bool autoSaveReport=settings.value("autoSaveReport",false).toBool(); ui->actionAutoSaveReport->setChecked(autoSaveReport); @@ -333,9 +335,10 @@ void MainWindow::readSettings() void MainWindow::writeSettings() const { QSettings settings("p-s team", "SRHDDumpReader"); + settings.setValue("shortSleep", shortSleep); settings.setValue("maxGenerationTime", maxGenerationTime); - settings.setValue("mapScale", (double)mapScale); + settings.setValue("mapWidth", mapWidth); settings.setValue("autoReload",ui->actionAutoReload->isChecked()); settings.setValue("autoSaveReport",ui->actionAutoSaveReport->isChecked()); @@ -686,7 +689,7 @@ void MainWindow::loadPresets() void MainWindow::updateMap() { - galaxyMap=galaxy.map(mapScale); + galaxyMap=galaxy.map(mapWidth); ui->mapImageLabel->setPixmap(QPixmap::fromImage(galaxyMap)); ui->mapImageLabel->resize(galaxyMap.size()); } @@ -713,7 +716,7 @@ void MainWindow::updateDumpArrows() void MainWindow::saveMap() { - if(mapScale>0.f) { + if(mapWidth>10) { QFileInfo fileInfo(_filename); QString filename=fileInfo.path()+'/'+fileInfo.completeBaseName()+"_map.png"; if(QFileInfo(filename).exists()) { diff --git a/MainWindow.h b/MainWindow.h index 7e5e56e..423de59 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -90,9 +90,9 @@ public slots: void showAbout(); void saveReport(); void saveAllReports(); - void setMapScale(double scale) + void setMapScale(int width) { - mapScale=scale; + mapWidth=width; updateMap(); } void loadNextDump(); @@ -193,7 +193,7 @@ private slots: FilterHorizontalHeaderView *planetsHeaderView; QTimer reloadTimer; - QDoubleSpinBox _mapScaleSpinBox{this}; + QSpinBox _mapScaleSpinBox{this}; QMenu reloadMenu; QMenu saveReportMenu; @@ -208,7 +208,7 @@ private slots: int maxGenerationTime=120000; int screenSaveLag=200; int shortSleep=25; - float mapScale=6.f; + unsigned mapWidth=800; QStringList planetsReportPresets; QStringList eqReportPresets; QMap minRowsPreset; diff --git a/main.cpp b/main.cpp index fc314a6..e2f655d 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include int main(int argc, char *argv[]) { @@ -16,6 +17,7 @@ int main(int argc, char *argv[]) RegisterHotKey((HWND)w.winId(), 101, 0, 0x75);//F6 QDir::setCurrent(QCoreApplication::applicationDirPath()); #endif + std::cout<<"devicePixelRatio()="<devicePixelRatio()<