Freeciv21
Develop your civilization from humble roots to a global empire
improvement_seller.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2023 Louis Moureaux <m_louis30@yahoo.com>
3  * SPDX-FileCopyrightText: Copyright (c) 1996-2022 Freeciv21 and Freeciv
4  * contributors
5  *
6  * SPDX-License-Identifier: GPLv3-or-later
7  */
8 
9 #include "improvement_seller.h"
10 
11 #include "citydlg_common.h"
12 #include "client_main.h"
13 #include "game.h"
14 #include "hudwidget.h"
15 #include "improvement.h"
16 #include "log.h"
17 
18 #include <QMenu>
19 
20 namespace freeciv {
21 
41 improvement_seller::improvement_seller(QWidget *parent, int city_id,
42  int improvement_id)
43  : m_city(city_id), m_improvement(improvement_id), m_parent(parent)
44 {
45 }
46 
51 {
52  if (!can_client_issue_orders()) {
53  return; // Kicked
54  }
55 
56  const auto city = game_city_by_number(m_city);
57  const auto building = improvement_by_number(m_improvement);
58 
59  fc_assert_ret(city != nullptr);
60  fc_assert_ret(building != nullptr);
61 
62  // Cannot sell it now
64  != TR_SUCCESS) {
65  return;
66  }
67 
68  const auto price = impr_sell_gold(building);
69  const auto buf =
70  QString(PL_("Sell %1 for %2 gold?", "Sell %1 for %2 gold?", price))
72  QString::number(price));
73 
74  auto ask = new hud_message_box(m_parent);
75  ask->setAttribute(Qt::WA_DeleteOnClose);
76 
77  ask->set_text_title(buf, (_("Sell Improvement?")));
78  ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Yes);
79  ask->setDefaultButton(QMessageBox::Cancel);
80  ask->button(QMessageBox::Yes)->setText(_("Yes Sell"));
81 
82  int city_id = m_city, improvement_id = m_improvement; // For lambda capture
83  QObject::connect(ask, &hud_message_box::accepted, [=] {
84  auto city = game_city_by_number(city_id);
85  if (!city || !can_client_issue_orders()) {
86  return;
87  }
88  city_sell_improvement(city, improvement_id);
89  });
90 
91  ask->show();
92 }
93 
100 QAction *improvement_seller::add_to_menu(QWidget *parent, QMenu *menu,
101  const city *city,
102  int improvement_id)
103 {
104  fc_assert_ret_val(parent != nullptr, nullptr);
105 
106  auto action =
107  menu->addAction(_("Sell Improvement"),
108  improvement_seller(parent, city->id, improvement_id));
109 
110  auto building = improvement_by_number(improvement_id);
111  action->setEnabled(
112  building && can_client_issue_orders()
114  == TR_SUCCESS);
115 
116  return action;
117 }
118 
119 } // namespace freeciv
const char * city_improvement_name_translation(const struct city *pcity, const struct impr_type *pimprove)
Return the extended name of the building.
Definition: city.cpp:635
int city_sell_improvement(struct city *pcity, Impr_type_id sell_id)
Change the production of a given city.
void operator()()
Asks for confirmation then sells the improvement.
improvement_seller(QWidget *parent, int city_id, int improvement_id)
Constructor.
static QAction * add_to_menu(QWidget *parent, QMenu *menu, const city *city, int improvement_id)
Adds a menu item to sell an improvement in a city.
struct civclient client
bool can_client_issue_orders()
Returns TRUE iff the client can issue orders (such as giving unit commands).
@ TR_SUCCESS
Definition: fc_types.h:1063
#define PL_(String1, String2, n)
Definition: fcintl.h:54
#define _(String)
Definition: fcintl.h:50
struct city * game_city_by_number(int id)
Often used function to get a city pointer from a city ID.
Definition: game.cpp:103
int impr_sell_gold(const struct impr_type *pimprove)
Returns the amount of gold received when this improvement is sold.
struct impr_type * improvement_by_number(const Impr_type_id id)
Returns the improvement type for the given index/ID.
enum test_result test_player_sell_building_now(struct player *pplayer, const city *pcity, const struct impr_type *pimprove)
Return TRUE iff the player can sell the given improvement from city.
#define fc_assert_ret(condition)
Definition: log.h:112
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
Definition: path.cpp:10
Definition: city.h:291
int id
Definition: city.h:296
struct connection conn
Definition: client_main.h:89
struct player * playing
Definition: connection.h:142