From 0a4f93ab2bc7d4f45bb97d25a953e474d79550f3 Mon Sep 17 00:00:00 2001 From: Gerald Combs Date: Wed, 4 Mar 2015 08:12:58 -0800 Subject: Qt: Show the full file path in the status bar. Add file_size_to_qstring and use it to show the file size. Add a "shrinkable" property to LabelStack. Make the info status shrinkable. Elide text so that long file paths don't widen the main window. Change-Id: Ieb1caaf7e016384609d41fcabaa63d8f7a293eff Bug: 10949 Reviewed-on: https://code.wireshark.org/review/7534 Petri-Dish: Gerald Combs Tested-by: Petri Dish Buildbot Reviewed-by: Gerald Combs --- ui/qt/capture_file_properties_dialog.cpp | 2 +- ui/qt/elided_label.cpp | 4 +++ ui/qt/label_stack.cpp | 42 +++++++++++++++++++++++++++++--- ui/qt/label_stack.h | 5 +++- ui/qt/main_status_bar.cpp | 5 ++-- ui/qt/main_status_bar.h | 4 +-- ui/qt/main_window_slots.cpp | 4 ++- ui/qt/qt_ui_utils.cpp | 10 ++++++-- ui/qt/qt_ui_utils.h | 16 +++++++++--- 9 files changed, 75 insertions(+), 17 deletions(-) (limited to 'ui') diff --git a/ui/qt/capture_file_properties_dialog.cpp b/ui/qt/capture_file_properties_dialog.cpp index 1950b3bfb6..54848c308a 100644 --- a/ui/qt/capture_file_properties_dialog.cpp +++ b/ui/qt/capture_file_properties_dialog.cpp @@ -163,7 +163,7 @@ QString CaptureFilePropertiesDialog::summaryToHtml() out << table_row_begin << table_vheader_tmpl.arg(tr("Length")) - << table_data_tmpl.arg(gchar_free_to_qstring(format_size(summary.file_length, format_size_unit_bytes))) + << table_data_tmpl.arg(file_size_to_qstring(summary.file_length)) << table_row_end; QString format_str = wtap_file_type_subtype_string(summary.file_type); diff --git a/ui/qt/elided_label.cpp b/ui/qt/elided_label.cpp index a07cb074b7..eb33cbccac 100644 --- a/ui/qt/elided_label.cpp +++ b/ui/qt/elided_label.cpp @@ -28,6 +28,8 @@ ElidedLabel::ElidedLabel(QWidget *parent) : QLabel(parent), small_text_(false) { + QFontMetrics fm(font()); + setMinimumWidth(fm.height() * 5); // em-widths } void ElidedLabel::setUrl(const QString &url) @@ -43,6 +45,8 @@ void ElidedLabel::resizeEvent(QResizeEvent *) void ElidedLabel::updateText() { + // XXX We should probably move text drawing to PaintEvent to match + // LabelStack. int fudged_width = small_text_ ? width() * 1.2 : width(); QString elided_text = fontMetrics().elidedText(full_text_, Qt::ElideMiddle, fudged_width); QString label_text = small_text_ ? "" : ""; diff --git a/ui/qt/label_stack.cpp b/ui/qt/label_stack.cpp index 2ccb9092c3..d88613be98 100644 --- a/ui/qt/label_stack.cpp +++ b/ui/qt/label_stack.cpp @@ -20,8 +20,10 @@ */ #include "label_stack.h" -#include + #include +#include +#include #include "tango_colors.h" @@ -32,12 +34,13 @@ const int temporary_flash_timeout_ = temporary_interval_ / 5; const int num_flashes_ = 3; LabelStack::LabelStack(QWidget *parent) : - QLabel(parent) + QLabel(parent), + temporary_ctx_(-1), + shrinkable_(false) { #ifdef Q_OS_MAC setAttribute(Qt::WA_MacSmallSize, true); #endif - temporary_ctx_ = -1; fillLabel(); connect(&temporary_timer_, SIGNAL(timeout()), this, SLOT(updateTemporaryStatus())); @@ -77,7 +80,7 @@ void LabelStack::fillLabel() { setText(si.text); } -void LabelStack::pushText(QString &text, int ctx) { +void LabelStack::pushText(const QString &text, int ctx) { popText(ctx); if (ctx == temporary_ctx_) { @@ -95,6 +98,18 @@ void LabelStack::pushText(QString &text, int ctx) { fillLabel(); } +void LabelStack::setShrinkable(bool shrinkable) +{ + shrinkable_ = shrinkable; + int min_width = 0; + + if (shrinkable) { + min_width = fontMetrics().height() * 5; // em-widths + } + setMinimumWidth(min_width); + fillLabel(); +} + void LabelStack::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) @@ -121,6 +136,25 @@ void LabelStack::contextMenuEvent(QContextMenuEvent *event) emit mousePressedAt(QPoint(event->globalPos()), Qt::RightButton); } +void LabelStack::paintEvent(QPaintEvent *event) +{ + if (!shrinkable_) { + QLabel::paintEvent(event); + return; + } + + // Other "elided label" examples draw the label text by hand, + // reimplementing QLabel::paintEvent. Disabling updates and letting + // QLabel do the work for us seems to work, however. + QString elided_text = fontMetrics().elidedText(text(), Qt::ElideMiddle, width()); + QString full_text = text(); + setUpdatesEnabled(false); + setText(elided_text); + QLabel::paintEvent(event); + setText(full_text); + setUpdatesEnabled(true); +} + void LabelStack::popText(int ctx) { QMutableListIterator iter(labels_); diff --git a/ui/qt/label_stack.h b/ui/qt/label_stack.h index ea052e21df..1adb057c0e 100644 --- a/ui/qt/label_stack.h +++ b/ui/qt/label_stack.h @@ -33,7 +33,8 @@ class LabelStack : public QLabel public: explicit LabelStack(QWidget *parent = 0); void setTemporaryContext(const int ctx); - void pushText(QString &text, int ctx); + void pushText(const QString &text, int ctx); + void setShrinkable(bool shrinkable = true); protected: void mousePressEvent(QMouseEvent *event); @@ -41,6 +42,7 @@ protected: void mouseDoubleClickEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void contextMenuEvent(QContextMenuEvent *event); + void paintEvent (QPaintEvent *event); private: typedef struct _StackItem { @@ -50,6 +52,7 @@ private: int temporary_ctx_; QList labels_; + bool shrinkable_; QTime temporary_epoch_; QTimer temporary_timer_; diff --git a/ui/qt/main_status_bar.cpp b/ui/qt/main_status_bar.cpp index 7da9087927..d3acd6af4d 100644 --- a/ui/qt/main_status_bar.cpp +++ b/ui/qt/main_status_bar.cpp @@ -130,6 +130,7 @@ MainStatusBar::MainStatusBar(QWidget *parent) : info_progress_hb->setContentsMargins(0, 0, 0, 0); info_status_.setTemporaryContext(STATUS_CTX_TEMPORARY); + info_status_.setShrinkable(true); info_progress_hb->addWidget(&expert_status_); info_progress_hb->addWidget(&comment_label_); @@ -282,7 +283,7 @@ void MainStatusBar::popFilterStatus() { info_status_.popText(STATUS_CTX_FILTER); } -void MainStatusBar::pushPacketStatus(QString &message) { +void MainStatusBar::pushPacketStatus(const QString &message) { packet_status_.pushText(message, STATUS_CTX_MAIN); } @@ -290,7 +291,7 @@ void MainStatusBar::popPacketStatus() { packet_status_.popText(STATUS_CTX_MAIN); } -void MainStatusBar::pushProfileStatus(QString &message) { +void MainStatusBar::pushProfileStatus(const QString &message) { profile_status_.pushText(message, STATUS_CTX_MAIN); } diff --git a/ui/qt/main_status_bar.h b/ui/qt/main_status_bar.h index 47d438e20e..49accc6ae9 100644 --- a/ui/qt/main_status_bar.h +++ b/ui/qt/main_status_bar.h @@ -76,9 +76,9 @@ public slots: void updateCaptureStatistics(capture_session * cap_session); private slots: - void pushPacketStatus(QString &message); + void pushPacketStatus(const QString &message); void popPacketStatus(); - void pushProfileStatus(QString &message); + void pushProfileStatus(const QString &message); void popProfileStatus(); void toggleBackground(bool enabled); void switchToProfile(); diff --git a/ui/qt/main_window_slots.cpp b/ui/qt/main_window_slots.cpp index 5e9b821183..979f7203f5 100644 --- a/ui/qt/main_window_slots.cpp +++ b/ui/qt/main_window_slots.cpp @@ -637,7 +637,9 @@ void MainWindow::captureFileReadFinished() { setForCapturedPackets(true); main_ui_->statusBar->popFileStatus(); - QString msg = QString().sprintf("%s", get_basename(capture_file_.capFile()->filename)); + QString msg = QString("%1 (%2)") + .arg(capture_file_.capFile()->filename) + .arg(file_size_to_qstring(capture_file_.capFile()->f_datalen)); main_ui_->statusBar->pushFileStatus(msg); emit setDissectedCaptureFile(capture_file_.capFile()); } diff --git a/ui/qt/qt_ui_utils.cpp b/ui/qt/qt_ui_utils.cpp index c4e33e3002..0bc9e39f0c 100644 --- a/ui/qt/qt_ui_utils.cpp +++ b/ui/qt/qt_ui_utils.cpp @@ -99,10 +99,16 @@ const QString val_ext_to_qstring(const guint32 val, value_string_ext *vse, const return val_qstr; } -const QString bits_s_to_qstring(const double val) +const QString bits_s_to_qstring(const double bits_s) { return gchar_free_to_qstring( - format_size(val, format_size_unit_none|format_size_prefix_si)); + format_size(bits_s, format_size_unit_none|format_size_prefix_si)); +} + +const QString file_size_to_qstring(const gint64 size) +{ + return gchar_free_to_qstring( + format_size(size, format_size_unit_bytes|format_size_prefix_si)); } void smooth_font_size(QFont &font) { diff --git a/ui/qt/qt_ui_utils.h b/ui/qt/qt_ui_utils.h index 85208aacc3..7176605044 100644 --- a/ui/qt/qt_ui_utils.h +++ b/ui/qt/qt_ui_utils.h @@ -118,7 +118,7 @@ const QString address_to_display_qstring(const struct _address *address); const QString val_to_qstring(const guint32 val, const struct _value_string *vs, const char *fmt) G_GNUC_PRINTF(3, 0); -/** Convert an value_string_ext to a QString using val_to_str_ext_wmem(). +/** Convert a value_string_ext to a QString using val_to_str_ext_wmem(). * * @param val The value to convert to string. * @param vse value_string_ext array. @@ -129,13 +129,21 @@ G_GNUC_PRINTF(3, 0); const QString val_ext_to_qstring(const guint32 val, struct _value_string_ext *vse, const char *fmt) G_GNUC_PRINTF(3, 0); -/** Convert bits per second value human-readable QString using format_size(). +/** Convert a bits per second value to a human-readable QString using format_size(). * * @param val The value to convert to string. * - * @return A QString representation of the data rate. + * @return A QString representation of the data rate in SI units. */ -const QString bits_s_to_qstring(const double val); +const QString bits_s_to_qstring(const double bits_s); + +/** Convert a file size value to a human-readable QString using format_size(). + * + * @param val The value to convert to string. + * + * @return A QString representation of the file size in SI units. + */ +const QString file_size_to_qstring(const gint64 size); /** * Round the current size of a font up to its next "smooth" size. -- cgit v1.2.3