Skip to content

Commit

Permalink
Take into account that the minimap can be hidden
Browse files Browse the repository at this point in the history
There is a menu item to hide the minimap. It was only toggling the minimap
without changing the layout of the panel at the bottom right, resulting in an
almost-empty widget still hiding as much of the map as before. Now, hiding the
minimap properly reduces the size of the bottom right widget, at the expense of
also hiding the zoom buttons located there.

The manual layout (i.e. without using QLayout) causes some trouble when widgets
are hidden or restored. Eventually I chose to post a LayoutRequest event by
hand to the widget doing the layout. If one day we write a proper QLayout for
the overlay widgets, we can remove this.

Closes longturn#1096.
  • Loading branch information
lmoureaux committed Jul 9, 2022
1 parent 797dedf commit 6e46e78
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 16 deletions.
4 changes: 2 additions & 2 deletions client/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,8 @@ void mr_menu::setup_menus()
minimap_status->setCheckable(true);
minimap_status->setShortcut(shortcut2key(SC_MINIMAP));
minimap_status->setChecked(true);
connect(minimap_status, &QAction::triggered,
queen()->minimap_panel->minimap(), &QWidget::setVisible);
connect(minimap_status, &QAction::triggered, queen()->minimap_panel,
&minimap_panel::set_minimap_visible);
osd_status = menu->addAction(_("Show new turn information"));
osd_status->setCheckable(true);
osd_status->setChecked(king()->qt_settings.show_new_turn_text);
Expand Down
7 changes: 6 additions & 1 deletion client/minimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ void minimap_view::update_image()
int minimap_view::heightForWidth(int width) const
{
const auto size = gui_options.overview.map->size();
return size.height() * width / size.width();
if (tileset_is_isometric(tileset)) {
// Traditional iso tilesets have more or less this aspect ratio
return size.height() * width / size.width() / 2;
} else {
return size.height() * width / size.width();
}
}

/**
Expand Down
21 changes: 21 additions & 0 deletions client/minimap_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ minimap_panel::minimap_panel(map_view *map, QWidget *parent)
connect(ui.turn_done, &QAbstractButton::clicked, top_bar_finish_turn);
}

/**
* Shows or hides the minimap
*/
void minimap_panel::set_minimap_visible(bool visible)
{
ui.minimap->setVisible(visible);
ui.zoom_in->setVisible(visible);
ui.zoom_reset->setVisible(visible);
ui.zoom_out->setVisible(visible);
ui.spacer->changeSize(0, 0, QSizePolicy::Minimum,
visible ? QSizePolicy::Expanding
: QSizePolicy::Minimum);
// See documentation for QSpacerItem::changeSize
ui.verticalLayout->invalidate();

// This isn't properly propagated by the map view. One day we'll have a
// proper QLayout...
QApplication::postEvent(queen()->game_tab_widget,
new QEvent(QEvent::LayoutRequest));
}

/**
* Update the timeout display. The timeout is the time until the turn ends.
*/
Expand Down
2 changes: 2 additions & 0 deletions client/minimap_panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class minimap_panel : public fcwidget {

void update_menu() override {}

void set_minimap_visible(bool visible);

/// Retrieves the minimap widget.
auto minimap() { return ui.minimap; }

Expand Down
12 changes: 12 additions & 0 deletions client/minimap_panel.ui
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
<height>300</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="autoFillBackground">
<bool>true</bool>
</property>
Expand Down Expand Up @@ -72,6 +78,12 @@
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
</layout>
Expand Down
27 changes: 15 additions & 12 deletions client/page_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,15 @@ void fc_game_tab_widget::init()
/**
Resize event for all game tab widgets
*/
void fc_game_tab_widget::resizeEvent(QResizeEvent *event)
bool fc_game_tab_widget::event(QEvent *event)
{
QSize size;
size = event->size();
if (C_S_RUNNING <= client_state()) {
if (C_S_RUNNING <= client_state()
&& (event->type() == QEvent::Resize
|| event->type() == QEvent::LayoutRequest)) {
auto size = event->type() == QEvent::Resize
? static_cast<QResizeEvent *>(event)->size()
: this->size();

map_canvas_resized(size.width(), size.height());
queen()->message->resize(
qRound((size.width() * king()->qt_settings.chat_fwidth)),
Expand All @@ -528,7 +532,7 @@ void fc_game_tab_widget::resizeEvent(QResizeEvent *event)
queen()->updateSidebarTooltips();
queen()->minimap_panel->turn_done()->setEnabled(
get_turn_done_button_state());
queen()->mapview_wdg->resize(event->size().width(), size.height());
queen()->mapview_wdg->resize(size.width(), size.height());
queen()->city_overlay->resize(queen()->mapview_wdg->size());
queen()->unitinfo_wdg->update_actions(nullptr);
queen()->civ_status->move(
Expand All @@ -540,17 +544,16 @@ void fc_game_tab_widget::resizeEvent(QResizeEvent *event)
*/
const auto max_size = QSize(std::max(300, size.width() / 4),
std::max(200, size.height() / 3));
const auto hint = queen()->minimap_panel->sizeHint();
// Try to keep aspect ratio
const auto width = std::min(max_size.width(), hint.width());
const auto height = std::min(
max_size.height(), queen()->minimap_panel->heightForWidth(width));
const auto panel_size = QSize(width, height);
const auto panel_size =
QLayout::closestAcceptableSize(queen()->minimap_panel, max_size);
const auto location = size - panel_size;
queen()->minimap_panel->move(location.width(), location.height());
queen()->minimap_panel->resize(panel_size);

return true;
}
event->setAccepted(true);

return QWidget::event(event);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion client/page_game.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class fc_game_tab_widget : public QStackedWidget {
void init();

protected:
void resizeEvent(QResizeEvent *event) override;
bool event(QEvent *event) override;
private slots:
void current_changed(int index);
};
Expand Down

0 comments on commit 6e46e78

Please sign in to comment.