Skip to content

Commit cf4971d

Browse files
committed
QtCommon v4.4.0
1 parent 3edc40d commit cf4971d

File tree

8 files changed

+149
-58
lines changed

8 files changed

+149
-58
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ project(QtCommon)
33

44
# Define version information
55
set(QTCOMMON_MAJOR_VERSION 4)
6-
set(QTCOMMON_MINOR_VERSION 3)
6+
set(QTCOMMON_MINOR_VERSION 4)
77
if (NOT QTCOMMON_PATCH_NUMBER)
88
set(QTCOMMON_PATCH_NUMBER 0)
99
endif ()

source/qt_common/custom_widgets/include_directories_view.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ void IncludeDirectoriesView::HandleIncludeFileLocationBrowseButtonClick(bool /*
129129

130130
while (should_show_find_directory_dialog)
131131
{
132-
QString selected_directory =
133-
QFileDialog::getExistingDirectory(this, tr(kStrIncludeDirDialogSelectDirTitle), latest_path.c_str(), QFileDialog::ShowDirsOnly);
132+
QString selected_directory = QDir::toNativeSeparators(
133+
QFileDialog::getExistingDirectory(this, tr(kStrIncludeDirDialogSelectDirTitle), latest_path.c_str(), QFileDialog::ShowDirsOnly));
134134

135135
// If the user did not select an entry, don't update anything, just exit the loop.
136136
if (selected_directory.isEmpty())
@@ -202,7 +202,7 @@ void IncludeDirectoriesView::OnListItemChanged(QListWidgetItem* item)
202202
// Process the newly-entered data.
203203
if (item != nullptr)
204204
{
205-
QString new_directory = item->text();
205+
QString new_directory = QDir::toNativeSeparators(item->text());
206206

207207
bool directory_exists = QDir(new_directory).exists();
208208
bool directory_duplicate = items_list_.contains(new_directory);
@@ -232,16 +232,22 @@ void IncludeDirectoriesView::OnListItemChanged(QListWidgetItem* item)
232232
}
233233

234234
editing_invalid_entry_ = true;
235-
}
236235

237-
// Update local data.
238-
if (item_row < items_list_.count())
239-
{
240-
items_list_[item_row] = new_directory;
236+
RemoveItem(item_row);
241237
}
242238
else
243239
{
244-
items_list_.append(new_directory);
240+
GetItemsListWidget()->item(item_row)->setText(new_directory);
241+
242+
// Update local data.
243+
if (item_row < items_list_.count())
244+
{
245+
items_list_[item_row] = new_directory;
246+
}
247+
else
248+
{
249+
items_list_.append(new_directory);
250+
}
245251
}
246252
}
247253

source/qt_common/custom_widgets/ordered_list_dialog.cpp

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -257,33 +257,52 @@ void OrderedListDialog::HandleMoveUpButtonClick(bool /* checked */)
257257
UpdateButtons();
258258
}
259259

260+
void OrderedListDialog::RemoveItem(int row)
261+
{
262+
if (row < ui_->itemsList->count())
263+
{
264+
QListWidgetItem* item = ui_->itemsList->takeItem(row);
265+
delete item;
266+
267+
// Update buttons.
268+
UpdateButtons();
269+
}
270+
271+
// If there is also a corresponding data entry, remove that too.
272+
if (row < items_list_.count())
273+
{
274+
items_list_.removeAt(row);
275+
}
276+
}
277+
278+
void OrderedListDialog::RemoveSelectedItem()
279+
{
280+
// Remove the selected item from the list widget.
281+
int current_index = ui_->itemsList->currentRow();
282+
283+
// Remove the selected item from the string list.
284+
if (current_index < ui_->itemsList->count())
285+
{
286+
RemoveItem(current_index);
287+
}
288+
289+
// If this was the last item in the list widget,
290+
// add a blank place holder.
291+
if (ui_->itemsList->count() == 0)
292+
{
293+
// Insert an empty item to the list widget and highlight it.
294+
InsertBlankItem();
295+
}
296+
}
297+
260298
void OrderedListDialog::HandleDeleteButtonClick(bool /* checked */)
261299
{
262300
// Display a confirmation message box.
263301
bool is_confirmation = ShowConfirmationMessageBox();
264302

265303
if (is_confirmation)
266304
{
267-
// Remove the selected item from the list widget.
268-
int current_index = ui_->itemsList->currentRow();
269-
QListWidgetItem* current_item = ui_->itemsList->takeItem(current_index);
270-
271-
// Remove the selected item from the string list.
272-
if (current_item != nullptr && current_index < items_list_.count())
273-
{
274-
items_list_.removeAt(current_index);
275-
276-
// Update buttons.
277-
UpdateButtons();
278-
}
279-
280-
// If this was the last item in the list widget,
281-
// add a blank place holder.
282-
if (ui_->itemsList->count() == 0)
283-
{
284-
// Insert an empty item to the list widget and highlight it.
285-
InsertBlankItem();
286-
}
305+
RemoveSelectedItem();
287306
}
288307
}
289308

@@ -470,7 +489,10 @@ QListWidget* OrderedListDialog::GetItemsListWidget()
470489
bool OrderedListDialog::ShowConfirmationMessageBox()
471490
{
472491
QMessageBox confirmation_dialog(this);
473-
confirmation_dialog.setWindowIcon(window_icon_);
492+
if (!window_icon_.isNull())
493+
{
494+
confirmation_dialog.setWindowIcon(window_icon_);
495+
}
474496
confirmation_dialog.setWindowTitle(kStrIncludeDirDialogDeleteBoxTitle);
475497
confirmation_dialog.setText(kStrIncludeDirDialogDeleteBoxMessage);
476498
confirmation_dialog.setIcon(QMessageBox::Question);

source/qt_common/custom_widgets/ordered_list_dialog.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ private slots:
8989
/// @brief Update tool tips.
9090
void UpdateToolTips();
9191

92+
/// @brief Removes item at specified row.
93+
/// @param [in] row The row to remove item at.
94+
void RemoveItem(int row);
95+
96+
/// @brief Removes the currently selected item.
97+
void RemoveSelectedItem();
98+
9299
/// @brief Handle to the Vertical PushButtons Layout of the dialog.
93100
/// @return pointer to the Vertical PushButtons Layout.
94101
QVBoxLayout* GetVerticalPushButtonsLayout();

source/qt_common/custom_widgets/ruler_widget.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void RulerWidget::PaintRulerBackground(QPainter* painter, const QRectF& rect)
3131
pen.setColor(QtCommon::QtUtils::ColorTheme::Get().GetCurrentThemeColors().ruler_edge_color);
3232
brush.setColor(QtCommon::QtUtils::ColorTheme::Get().GetCurrentThemeColors().ruler_background_color);
3333

34-
pen.setCosmetic(true); // Don't scale.
34+
pen.setCosmetic(true); // Don't scale.
3535

3636
painter->setPen(pen);
3737
painter->setBrush(brush);
@@ -71,7 +71,7 @@ void RulerWidget::PaintRuler(QPainter* painter,
7171
auto pen = painter->pen();
7272

7373
pen.setColor(QtCommon::QtUtils::ColorTheme::Get().GetCurrentThemeColors().ruler_marker_color);
74-
pen.setCosmetic(true); // Don't scale.
74+
pen.setCosmetic(true); // Don't scale.
7575

7676
painter->setPen(pen);
7777

@@ -103,24 +103,27 @@ void RulerWidget::PaintRuler(QPainter* painter,
103103
// values of 0, 10, 20, 30 .. may cause the ruler to be too squashed, so maybe having ruler
104104
// segments of 0, 100, 200 (more spaced out) may be better
105105
const double lower_limit = kMinimumTickStep;
106-
while (tick_step < lower_limit)
106+
while (tick_step < lower_limit && tick_step > 0)
107107
{
108108
// ie 10, 100, 1000, 10000 etc.
109109
time_period *= 10;
110-
num_sections = (markers_per_section * max_clock_time) / time_period;
111-
tick_step = ruler_length / num_sections;
110+
double num_time_labels = max_clock_time / time_period;
111+
num_sections = static_cast<double>(markers_per_section) * num_time_labels;
112+
113+
tick_step = ruler_length / num_sections;
112114
}
113115

114116
// Scale the ticks back down in the case where the gap between them is too large. Up to this
115117
// part, the scaling will be in base 10 but maybe the gap between 10000 and 20000 is too large.
116118
// In this case, the tick_step is halved so this goes down to 10000, 15000, 20000.
117119
const double upper_limit = kMaximumTickStep;
118-
while (tick_step > upper_limit)
120+
while (tick_step > upper_limit && time_period > 0)
119121
{
120122
time_period /= 2;
121-
//Q_ASSERT(time_period > 0);
122-
num_sections = (markers_per_section * max_clock_time) / time_period;
123-
tick_step = ruler_length / num_sections;
123+
double num_time_labels = max_clock_time / time_period;
124+
num_sections = static_cast<double>(markers_per_section) * num_time_labels;
125+
126+
tick_step = ruler_length / num_sections;
124127
}
125128

126129
uint64_t count = 0;
@@ -157,7 +160,7 @@ void RulerWidget::PaintRuler(QPainter* painter,
157160
QTransform wt = painter->worldTransform();
158161
wt.translate(x_pos, 0);
159162
painter->setWorldTransform(wt);
160-
painter->drawText(1, top - (marker_unit_height * 2), QtCommon::QtUtils::ClockToTimeUnit(clock_label, unit_type));
163+
painter->drawText(1, top - (marker_unit_height * 2), QtCommon::QtUtils::ClockToTimeUnit(clock_label, unit_type, scale_increment));
161164
wt.translate(-x_pos, 0);
162165
painter->setWorldTransform(wt);
163166
}

source/qt_common/custom_widgets/timeline_view.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ bool TimelineView::ZoomInClk(qint64 begin_clk, qint64 end_clk)
395395
end_clk += half_width;
396396
}
397397

398+
// Recompute the range in case of clamps
399+
selected_clock_range = end_clk - begin_clk;
400+
398401
// limit zoom level based on resolution of the scene / scroll bar
399402
uint64_t minimum_range = (ruler_config_.max_time * width()) / selected_clock_range;
400403
if (minimum_range > INT_MAX)
@@ -412,9 +415,6 @@ bool TimelineView::ZoomInClk(qint64 begin_clk, qint64 end_clk)
412415
// Clamp to within range
413416
ClampClocks(begin_clk, end_clk);
414417

415-
// Recompute the range in case of clamps
416-
selected_clock_range = end_clk - begin_clk;
417-
418418
// Compute a factor to scale the ruler by
419419
const qint64 visible_clock_range = viewable_end_clock_ - viewable_start_clock_;
420420
const double zoom_factor = (double)visible_clock_range / (double)selected_clock_range;

source/qt_common/utils/qt_util.cpp

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,56 @@ namespace QtCommon
366366
return result;
367367
}
368368

369-
QString QtUtils::ClockToTimeUnit(double clk, int unit_type)
369+
static constexpr double kClockMultiplier = 1000000000.0;
370+
371+
// @brief Get precision based on scale.
372+
//
373+
// As the ruler is zoomed in, more precision will be needed between the graduations.
374+
//
375+
// @param [in] scale The time scale. The smaller the scale, the larger the precision needed.
376+
//
377+
// @return The precision, as in the number of decimal places needed.
378+
static int GetPrecision(double scale)
379+
{
380+
constexpr int kMinPrecision = 2;
381+
constexpr int kMaxPrecision = 6;
382+
383+
// Some error checking on the input.
384+
if (scale < 0.001)
385+
{
386+
return kMaxPrecision;
387+
}
388+
389+
double precision = std::log10(kClockMultiplier / scale);
390+
if (precision < 2.0)
391+
{
392+
return kMinPrecision;
393+
}
394+
else if (precision > 6.0)
395+
{
396+
return kMaxPrecision;
397+
}
398+
return static_cast<int>(precision);
399+
}
400+
401+
// @brief Get the fractional part of the time passed in.
402+
//
403+
// The fractional part is any part coming after the decimal point when displaying larger time
404+
// values, for example 6,491ms may be displayed as 6.491s, so 491 would be returned.
405+
//
406+
// @param [in] time The time, in clock cycles
407+
// @param [in] scale The time scale. The smaller the scale, the larger the precision needed.
408+
//
409+
// @return The fractional part of the time.
410+
static uint64_t GetFraction(double time, int scale)
411+
{
412+
double power = pow(10, scale);
413+
double time_scale = kClockMultiplier / power;
414+
uint64_t fraction = static_cast<uint64_t>(fmod((time / time_scale), power));
415+
return fraction;
416+
}
417+
418+
QString QtUtils::ClockToTimeUnit(double clk, int unit_type, double scale)
370419
{
371420
double time = clk;
372421

@@ -397,31 +446,34 @@ namespace QtCommon
397446
case kTimeUnitTypeSecond:
398447
{
399448
out.setRealNumberPrecision(0);
400-
uint64_t secs = (uint64_t)(time / 1000000000.0);
401-
uint64_t ms = (uint64_t)fmod((time / 10000000.0), 100);
402-
out << QString::number(secs).rightJustified(2, '0') << "." << QString::number(ms).rightJustified(2, '0') << "s";
449+
uint64_t secs = static_cast<uint64_t>(time / kClockMultiplier);
450+
int precision = GetPrecision(scale);
451+
uint64_t fraction = GetFraction(time, precision);
452+
out << QString::number(secs).rightJustified(2, '0') << "." << QString::number(fraction).rightJustified(precision, '0') << "s";
403453
}
404454
break;
405455

406456
case kTimeUnitTypeMinute:
407457
{
408458
out.setRealNumberPrecision(0);
409-
uint64_t mins = (uint64_t)(time / 60000000000.0);
410-
uint64_t secs = (uint64_t)fmod((time / 1000000000.0), 60);
411-
uint64_t ms = (uint64_t)fmod((time / 10000000.0), 100);
412-
out << mins << "m " << QString::number(secs).rightJustified(2, '0') << "." << QString::number(ms).rightJustified(2, '0') << "s";
459+
uint64_t mins = static_cast<uint64_t>(time / 60000000000.0);
460+
uint64_t secs = static_cast<uint64_t>(fmod((time / kClockMultiplier), 60));
461+
int precision = GetPrecision(scale);
462+
uint64_t fraction = GetFraction(time, precision);
463+
out << mins << "m " << QString::number(secs).rightJustified(2, '0') << "." << QString::number(fraction).rightJustified(precision, '0') << "s";
413464
}
414465
break;
415466

416467
case kTimeUnitTypeHour:
417468
{
418469
out.setRealNumberPrecision(0);
419-
uint64_t hours = static_cast<uint64_t>(time / (60 * 60 * 1000000000.0));
420-
uint64_t mins = static_cast<uint64_t>(fmod((time / (60 * 1000000000.0)), 60));
421-
uint64_t secs = static_cast<uint64_t>(fmod((time / 1000000000.0), 60));
422-
uint64_t fraction = static_cast<uint64_t>(fmod((time / 10000.0), 100000));
470+
uint64_t hours = static_cast<uint64_t>(time / (60 * 60 * kClockMultiplier));
471+
uint64_t mins = static_cast<uint64_t>(fmod((time / (60 * kClockMultiplier)), 60));
472+
uint64_t secs = static_cast<uint64_t>(fmod((time / kClockMultiplier), 60));
473+
int precision = GetPrecision(scale);
474+
uint64_t fraction = GetFraction(time, precision);
423475
out << hours << ":" << QString::number(mins).rightJustified(2, '0') << ":" << QString::number(secs).rightJustified(2, '0') << "."
424-
<< QString::number(fraction).leftJustified(5, '0');
476+
<< QString::number(fraction).rightJustified(precision, '0');
425477
}
426478
break;
427479

source/qt_common/utils/qt_util.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ namespace QtCommon
9898
/// Utility function to convert a clock to a time unit and output as string.
9999
/// \param clk input clock to convert
100100
/// \param unit_type unit type (clk, ns, us, ms)
101+
/// \param scale The time value scale. The smaller the scale, the larger the precision needed.
101102
/// \return a string representing a clock value
102-
QString ClockToTimeUnit(double clk, int unit_type);
103+
QString ClockToTimeUnit(double clk, int unit_type, double scale = 0.0);
103104

104105
/// Utility function to convert a uint64 value to capitalized and aligned str
105106
/// \param value 64-bit value

0 commit comments

Comments
 (0)