Author: mir3x Date: Wed Nov 2 11:14:15 2016 New Revision: 34325 URL: http://svn.gna.org/viewcvs/freeciv?rev=34325&view=rev Log: Qt client - added cropping transparent borders
See patch #7895 Modified: branches/S2_6/client/gui-qt/canvas.cpp branches/S2_6/client/gui-qt/canvas.h branches/S2_6/client/gui-qt/citydlg.cpp branches/S2_6/client/gui-qt/citydlg.h branches/S2_6/client/gui-qt/dialogs.cpp branches/S2_6/client/gui-qt/dialogs.h branches/S2_6/client/gui-qt/hudwidget.cpp branches/S2_6/data/themes/gui-qt/NightStalker/resource.qss Modified: branches/S2_6/client/gui-qt/canvas.cpp URL: http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/canvas.cpp?rev=34325&r1=34324&r2=34325&view=diff ============================================================================== --- branches/S2_6/client/gui-qt/canvas.cpp (original) +++ branches/S2_6/client/gui-qt/canvas.cpp Wed Nov 2 11:14:15 2016 @@ -349,4 +349,37 @@ return qf; } - +/**************************************************************************** + Return rectangle containing pure image (crops transparency) +****************************************************************************/ +QRect zealous_crop_rect(QImage &p) +{ + int r, t, b, l; + + l = p.width(); + r = 0; + t = p.height(); + b = 0; + for (int y = 0; y < p.height(); ++y) { + QRgb *row = (QRgb *)p.scanLine(y); + bool row_filled = false; + int x; + + for (x = 0; x < p.width(); ++x) { + if (qAlpha(row[x])) { + row_filled = true; + r = qMax(r, x); + if (l > x) { + l = x; + x = r; + } + } + } + if (row_filled) { + t = qMin(t, y); + b = y; + } + } + return QRect(l, t, r - l, b - t); +} + Modified: branches/S2_6/client/gui-qt/canvas.h URL: http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/canvas.h?rev=34325&r1=34324&r2=34325&view=diff ============================================================================== --- branches/S2_6/client/gui-qt/canvas.h (original) +++ branches/S2_6/client/gui-qt/canvas.h Wed Nov 2 11:14:15 2016 @@ -31,5 +31,5 @@ struct canvas *qtg_canvas_create(int width, int height); void pixmap_copy(QPixmap *dest, QPixmap *src, int src_x, int src_y, int dest_x, int dest_y, int width, int height); - +QRect zealous_crop_rect(QImage &p); #endif /* FC__CANVAS_H */ Modified: branches/S2_6/client/gui-qt/citydlg.cpp URL: http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/citydlg.cpp?rev=34325&r1=34324&r2=34325&view=diff ============================================================================== --- branches/S2_6/client/gui-qt/citydlg.cpp (original) +++ branches/S2_6/client/gui-qt/citydlg.cpp Wed Nov 2 11:14:15 2016 @@ -19,8 +19,10 @@ #include <QApplication> #include <QDesktopWidget> #include <QHeaderView> +#include <QImage> #include <QMessageBox> #include <QRadioButton> +#include <QRect> #include <QScrollArea> #include <QScrollBar> #include <QSplitter> @@ -504,18 +506,23 @@ unit_item::unit_item(QWidget *parent, struct unit *punit, bool supp, int hppy_cost) : QLabel() { + happy_cost = hppy_cost; + QImage cropped_img; + QImage img; + QPixmap pix; + QRect crop; + qunit = punit; + struct canvas *unit_pixmap; + setParent(parent); - happy_cost = hppy_cost; supported = supp; - unit_pixmap = NULL; - qunit = punit; if (punit) { if (supported) { - unit_pixmap = qtg_canvas_create(tileset_full_tile_width(tileset), + unit_pixmap = qtg_canvas_create(tileset_unit_width(tileset), tileset_unit_with_upkeep_height(tileset)); } else { - unit_pixmap = qtg_canvas_create(tileset_full_tile_width(tileset), + unit_pixmap = qtg_canvas_create(tileset_unit_width(tileset), tileset_unit_height(tileset)); } @@ -527,11 +534,25 @@ tileset_unit_layout_offset_y(tileset), punit->upkeep, happy_cost); } - } - + } else { + unit_pixmap = qtg_canvas_create(10, 10); + unit_pixmap->map_pixmap.fill(Qt::transparent); + } + + img = unit_pixmap->map_pixmap.toImage(); + crop = zealous_crop_rect(img); + cropped_img = img.copy(crop); + if (tileset_is_isometric(tileset) == true) { + unit_img = cropped_img.scaledToHeight(tileset_unit_width(tileset) + * 0.6, Qt::SmoothTransformation); + } else { + unit_img = cropped_img.scaledToHeight(tileset_unit_width(tileset), + Qt::SmoothTransformation); + } + qtg_canvas_free(unit_pixmap); create_actions(); - setFixedWidth(unit_pixmap->map_pixmap.width() + 4); - setFixedHeight(unit_pixmap->map_pixmap.height()); + setFixedWidth(unit_img.width() + 4); + setFixedHeight(unit_img.height()); setToolTip(unit_description(qunit)); } @@ -540,7 +561,7 @@ ****************************************************************************/ void unit_item::init_pix() { - setPixmap(unit_pixmap->map_pixmap); + setPixmap(QPixmap::fromImage(unit_img)); update(); } @@ -549,9 +570,6 @@ ****************************************************************************/ unit_item::~unit_item() { - if (unit_pixmap) { - qtg_canvas_free(unit_pixmap); - } } /**************************************************************************** @@ -785,30 +803,17 @@ ****************************************************************************/ void unit_item::enterEvent(QEvent *event) { - if (unit_pixmap) { - delete unit_pixmap; - } - - if (qunit) { - if (supported) { - unit_pixmap = qtg_canvas_create(tileset_full_tile_width(tileset), - tileset_unit_with_upkeep_height(tileset)); - } else { - unit_pixmap = qtg_canvas_create(tileset_full_tile_width(tileset), - tileset_unit_height(tileset)); - } - - unit_pixmap->map_pixmap.fill(QColor(palette().color(QPalette::Highlight))); - put_unit(qunit, unit_pixmap, 1.0, 0, 0); - - if (supported) { - put_unit_city_overlays(qunit, unit_pixmap, 0, - tileset_unit_layout_offset_y(tileset), - qunit->upkeep, happy_cost); - } - } - - init_pix(); + QImage temp_img(unit_img.size(), QImage::Format_ARGB32_Premultiplied); + QPainter p; + + p.begin(&temp_img); + p.fillRect(0, 0, unit_img.width(), unit_img.height(), + QColor(palette().color(QPalette::Highlight))); + p.drawImage(0, 0, unit_img); + p.end(); + + setPixmap(QPixmap::fromImage(temp_img)); + update(); } /**************************************************************************** @@ -816,29 +821,6 @@ ****************************************************************************/ void unit_item::leaveEvent(QEvent *event) { - if (unit_pixmap) { - delete unit_pixmap; - } - - if (qunit) { - if (supported) { - unit_pixmap = qtg_canvas_create(tileset_full_tile_width(tileset), - tileset_unit_with_upkeep_height(tileset)); - } else { - unit_pixmap = qtg_canvas_create(tileset_full_tile_width(tileset), - tileset_unit_height(tileset)); - } - - unit_pixmap->map_pixmap.fill(Qt::transparent); - put_unit(qunit, unit_pixmap, 1.0, 0, 0); - - if (supported) { - put_unit_city_overlays(qunit, unit_pixmap, 0, - tileset_unit_layout_offset_y(tileset), - qunit->upkeep, happy_cost); - } - } - init_pix(); } @@ -923,11 +905,11 @@ layout->addWidget(ui, 0, Qt::AlignVCenter); } - h = tileset_unit_height(tileset) + 6; - if (supports) { - h = tileset_unit_with_upkeep_height(tileset) + 6; - } - + if (tileset_is_isometric(tileset)) { + h = tileset_unit_width(tileset) * 0.7 + 6; + } else { + h = tileset_unit_width(tileset) + 6; + } if (unit_list.count() > 0) { parentWidget()->parentWidget()->setFixedHeight(city_dlg->scroll_height + h); Modified: branches/S2_6/client/gui-qt/citydlg.h URL: http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/citydlg.h?rev=34325&r1=34324&r2=34325&view=diff ============================================================================== --- branches/S2_6/client/gui-qt/citydlg.h (original) +++ branches/S2_6/client/gui-qt/citydlg.h Wed Nov 2 11:14:15 2016 @@ -50,6 +50,8 @@ #include <QTableWidget> #include <QToolTip> +class QImage; + QString get_tooltip(QVariant qvar); QString get_tooltip_improvement(impr_type *building); QString get_tooltip_unit(struct unit_type *unit); @@ -119,7 +121,7 @@ private: struct unit *qunit; - struct canvas *unit_pixmap; + QImage unit_img; void contextMenuEvent(QContextMenuEvent *ev); void create_actions(); int happy_cost; Modified: branches/S2_6/client/gui-qt/dialogs.cpp URL: http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/dialogs.cpp?rev=34325&r1=34324&r2=34325&view=diff ============================================================================== --- branches/S2_6/client/gui-qt/dialogs.cpp (original) +++ branches/S2_6/client/gui-qt/dialogs.cpp Wed Nov 2 11:14:15 2016 @@ -18,8 +18,10 @@ // Qt #include <QComboBox> #include <QHeaderView> +#include <QImage> #include <QMessageBox> #include <QRadioButton> +#include <QRect> #include <QTableWidgetItem> #include <QTextEdit> @@ -2431,7 +2433,7 @@ } move(final_p.x(), final_p.y() - height()); setFocus(); - + QTimer::singleShot(10, this, SLOT(update_img())); } /**************************************************************** Destructor for unit select @@ -2448,17 +2450,22 @@ *****************************************************************/ void units_select::create_pixmap() { - struct unit *punit; - QPixmap *tmp_pix; - struct canvas *unit_pixmap; + int a; + int rate, f; + int x, y, i; + QFontMetrics fm(info_font); + QImage cropped_img; + QImage img; QList <QPixmap*>pix_list; QPainter p; + QPen pen; + QPixmap pixc; + QPixmap *pixp; + QPixmap *tmp_pix; + QRect crop; QString str; - int x, y, i; - int rate, f; - int a; - QPen pen; - QFontMetrics fm(info_font); + struct canvas *unit_pixmap; + struct unit *punit; if (pix != NULL) { delete pix; @@ -2468,8 +2475,13 @@ update_units(); if (unit_list.count() > 0) { punit = unit_list.at(0); - item_size.setWidth(tileset_full_tile_width(tileset)); - item_size.setHeight(tileset_tile_height(tileset) * 3 / 2); + if (tileset_is_isometric(tileset) == false) { + item_size.setWidth(tileset_unit_width(tileset)); + item_size.setHeight(tileset_unit_width(tileset)); + } else { + item_size.setWidth(tileset_unit_width(tileset) * 0.7); + item_size.setHeight(tileset_unit_width(tileset) * 0.7); + } more = false; if (h_pix != nullptr) { delete h_pix; @@ -2492,11 +2504,28 @@ } pix->fill(Qt::transparent); foreach(punit, unit_list) { - unit_pixmap = qtg_canvas_create(tileset_full_tile_width(tileset), - tileset_tile_height(tileset) * 3 / 2); + unit_pixmap = qtg_canvas_create(tileset_unit_width(tileset), + tileset_unit_height(tileset)); unit_pixmap->map_pixmap.fill(Qt::transparent); put_unit(punit, unit_pixmap, 1.0, 0, 0); - pix_list.push_back(&unit_pixmap->map_pixmap); + img = unit_pixmap->map_pixmap.toImage(); + crop = zealous_crop_rect(img); + cropped_img = img.copy(crop); + if (tileset_is_isometric(tileset) == false) { + img = cropped_img.scaled(tileset_unit_width(tileset), + tileset_unit_width(tileset), + Qt::KeepAspectRatio, + Qt::SmoothTransformation); + } else { + img = cropped_img.scaled(tileset_unit_width(tileset) * 0.7, + tileset_unit_width(tileset) * 0.7, + Qt::KeepAspectRatio, + Qt::SmoothTransformation); + } + pixc = QPixmap::fromImage(img); + pixp = new QPixmap(pixc); + pix_list.push_back(pixp); + qtg_canvas_free(unit_pixmap); } a = qMin(item_size.width() / 4, 12); x = 0, y = -item_size.height(), i = -1; @@ -2595,6 +2624,16 @@ } /**************************************************************** + Update image, because in constructor theme colors + are uninitialized in QPainter +*****************************************************************/ +void units_select::update_img() +{ + create_pixmap(); + update(); +} + +/**************************************************************** Redirected paint event *****************************************************************/ void units_select::paint(QPainter *painter, QPaintEvent *event) @@ -2751,14 +2790,6 @@ QWidget::keyPressEvent(event); } -/*************************************************************************** - Change the fonts when needed -***************************************************************************/ -void units_select::update_font(const QString& name, const QFont& font) -{ - // FIXME Updating fonts isn't practical because the size of the widget - // will change -> its position too. -} /*************************************************************************** Set current diplo dialog Modified: branches/S2_6/client/gui-qt/dialogs.h URL: http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/dialogs.h?rev=34325&r1=34324&r2=34325&view=diff ============================================================================== --- branches/S2_6/client/gui-qt/dialogs.h (original) +++ branches/S2_6/client/gui-qt/dialogs.h Wed Nov 2 11:14:15 2016 @@ -188,9 +188,9 @@ void mouseMoveEvent(QMouseEvent *event); void wheelEvent(QWheelEvent *event); void closeEvent(QCloseEvent *event); +private slots: + void update_img(); private: - void update_font(const QString &name, const QFont &font); - bool more; int show_line; int highligh_num; Modified: branches/S2_6/client/gui-qt/hudwidget.cpp URL: http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/hudwidget.cpp?rev=34325&r1=34324&r2=34325&view=diff ============================================================================== --- branches/S2_6/client/gui-qt/hudwidget.cpp (original) +++ branches/S2_6/client/gui-qt/hudwidget.cpp Wed Nov 2 11:14:15 2016 @@ -374,11 +374,15 @@ hud_units::hud_units(QWidget *parent) : QFrame(parent) { QVBoxLayout *vbox; + QVBoxLayout *unit_lab; setParent(parent); main_layout = new QHBoxLayout; vbox = new QVBoxLayout; + unit_lab = new QVBoxLayout; + unit_lab->setContentsMargins(6, 9, 0, 3); vbox->setSpacing(0); - main_layout->addWidget(&unit_label); + unit_lab->addWidget(&unit_label); + main_layout->addLayout(unit_lab); main_layout->addWidget(&tile_label); unit_icons = new unit_actions(this, nullptr); vbox->addWidget(&text_label); @@ -428,19 +432,22 @@ ****************************************************************************/ void hud_units::update_actions(unit_list *punits) { - QString text_str; - struct city *pcity; - struct unit *punit; - struct player *owner; - struct canvas *unit_pixmap; - struct canvas *tile_pixmap; - QPixmap pix; + int num; + int wwidth; QFont font = *fc_font::instance()->get_font(fonts::notify_label); QFontMetrics *fm; + QImage cropped_img; + QImage img; + QPixmap pix; + QRect crop; QString mp; - int wwidth; - int num; QString snum; + QString text_str; + struct canvas *tile_pixmap; + struct canvas *unit_pixmap; + struct city *pcity; + struct player *owner; + struct unit *punit; punit = head_of_units_in_focus(); if (punit == nullptr) { @@ -494,16 +501,16 @@ fm = new QFontMetrics(font); text_label.setFixedWidth(fm->width(text_str) + 3); delete fm; - if (tileset_is_isometric(tileset)) { - unit_pixmap = qtg_canvas_create(tileset_full_tile_width(tileset), - tileset_tile_height(tileset) * 3 / 2); - } else { - unit_pixmap = qtg_canvas_create(tileset_full_tile_width(tileset), - tileset_tile_height(tileset)); - } + + unit_pixmap = qtg_canvas_create(tileset_unit_width(tileset), + tileset_unit_height(tileset)); unit_pixmap->map_pixmap.fill(Qt::transparent); put_unit(punit, unit_pixmap, 1, 0, 0); - pix = (&unit_pixmap->map_pixmap)->scaledToHeight(height()); + img = unit_pixmap->map_pixmap.toImage(); + crop = zealous_crop_rect(img); + cropped_img = img.copy(crop); + img = cropped_img.scaledToHeight(height(), Qt::SmoothTransformation); + pix = QPixmap::fromImage(img); wwidth = 2 * 3 + pix.width(); unit_label.setPixmap(pix); if (tileset_is_isometric(tileset)) { @@ -515,7 +522,12 @@ } tile_pixmap->map_pixmap.fill(QColor(0 , 0 , 0 , 0)); put_terrain(punit->tile, tile_pixmap, 1.0, 0, 0); - pix = (&tile_pixmap->map_pixmap)->scaledToHeight(height()); + img = tile_pixmap->map_pixmap.toImage(); + crop = zealous_crop_rect(img); + cropped_img = img.copy(crop); + img = cropped_img.scaledToWidth(wwidth - 10, + Qt::SmoothTransformation); + pix = QPixmap::fromImage(img); tile_label.setPixmap(pix); unit_label.setToolTip(popup_info_text(punit->tile)); tile_label.setToolTip(popup_info_text(punit->tile)); Modified: branches/S2_6/data/themes/gui-qt/NightStalker/resource.qss URL: http://svn.gna.org/viewcvs/freeciv/branches/S2_6/data/themes/gui-qt/NightStalker/resource.qss?rev=34325&r1=34324&r2=34325&view=diff ============================================================================== --- branches/S2_6/data/themes/gui-qt/NightStalker/resource.qss (original) +++ branches/S2_6/data/themes/gui-qt/NightStalker/resource.qss Wed Nov 2 11:14:15 2016 @@ -708,7 +708,7 @@ units_select { background-color: rgba(0, 0, 0, 135); color: white; - selection-color: rgba(25, 25, 95, 135); + selection-color: #3399FF; border: 2px solid #0000AA; } @@ -781,7 +781,11 @@ background: #2704b2; } - +hud_unit_loader::item:hover { + border: none; + padding: 0 0; + background: #3399FF; +} fc_sidewidget { color: white; _______________________________________________ Freeciv-commits mailing list Freeciv-commits@gna.org https://mail.gna.org/listinfo/freeciv-commits