Skip to content

Commit

Permalink
Fix map rendering to be dpi-agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
Mykola Dimura authored and Mykola Dimura committed May 29, 2016
1 parent 55b04f9 commit e94dd90
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
14 changes: 9 additions & 5 deletions Galaxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned,QString> starIdToBases;
Expand Down Expand Up @@ -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<unsigned,QPointF> 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();
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Galaxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 12 additions & 9 deletions MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
}
Expand All @@ -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()) {
Expand Down
8 changes: 4 additions & 4 deletions MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -193,7 +193,7 @@ private slots:
FilterHorizontalHeaderView *planetsHeaderView;

QTimer reloadTimer;
QDoubleSpinBox _mapScaleSpinBox{this};
QSpinBox _mapScaleSpinBox{this};

QMenu reloadMenu;
QMenu saveReportMenu;
Expand All @@ -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<QString,int> minRowsPreset;
Expand Down
2 changes: 2 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <QApplication>
#include <QTranslator>
#include <QDir>
#include <iostream>

int main(int argc, char *argv[])
{
Expand All @@ -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()="<<qGuiApp->devicePixelRatio()<<std::endl;
w.show();

return a.exec();
Expand Down

0 comments on commit e94dd90

Please sign in to comment.