Skip to content

Commit

Permalink
fixes #114 long topic names cause strange behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ablasdel committed Aug 16, 2013
1 parent eca0846 commit dd6f42d
Showing 1 changed file with 49 additions and 8 deletions.
57 changes: 49 additions & 8 deletions rqt_bag/src/rqt_bag/timeline_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def __init__(self):
self._topic_font.setPointSize(self._topic_font_size)
self._topic_font.setBold(False)
self._topic_vertical_padding = 4
self._topic_name_max_percent = 25.0 # percentage of the horiz space that can be used for topic display

# Time Rendering
self._time_tick_height = 5
Expand Down Expand Up @@ -279,14 +280,56 @@ def paint(self, painter, option, widget):

# Drawing Functions

def _qfont_width(self, name):
return QFontMetrics(self._topic_font).width(name)

def _trimmed_topic_name(self, topic_name):
"""
This function trims the topic name down to a reasonable percentage of the viewable scene area
"""
allowed_width = self._scene_width * (self._topic_name_max_percent / 100.0)
allowed_width = allowed_width - self._topic_name_spacing - self._margin_left
trimmed_return = topic_name
if allowed_width < self._qfont_width(topic_name):
# We need to trim the topic
trimmed = ''
split_name = topic_name.split('/')
split_name = filter(lambda a: a != '', split_name)
# Save important last element of topic name provided it is small
popped_last = False
if self._qfont_width(split_name[-1]) < .5 * allowed_width:
popped_last = True
last_item = split_name[-1]
split_name = split_name[:-1]
allowed_width = allowed_width - self._qfont_width(last_item)
# Shorten and add remaining items keeping lenths roughly equal
for item in split_name:
if self._qfont_width(item) > allowed_width / float(len(split_name)):
trimmed_item = item[:-3] + '..'
while self._qfont_width(trimmed_item) > allowed_width / float(len(split_name)):
if len(trimmed_item) >= 3:
trimmed_item = trimmed_item[:-3] + '..'
else:
break
trimmed = trimmed + '/' + trimmed_item
else:
trimmed = trimmed + '/' + item
if popped_last:
trimmed = trimmed + '/' + last_item
trimmed = trimmed[1:]
trimmed_return = trimmed
return trimmed_return

def _layout(self):
"""
Recalculates the layout of the of the timeline to take into account any changes that have occured
"""
self._scene_width = self.scene().views()[0].size().width()

# Calculate history left and history width
max_topic_name_width = -1
for topic in self.topics:
topic_width = QFontMetrics(self._topic_font).width(topic)
topic_width = self._qfont_width(self._trimmed_topic_name(topic))
if max_topic_name_width <= topic_width:
max_topic_name_width = topic_width

Expand All @@ -299,11 +342,9 @@ def _layout(self):

# Update the timeline boundries
new_history_left = self._margin_left + max_topic_name_width + self._topic_name_spacing
new_history_width = self.scene().views()[0].size().width() - new_history_left - self._margin_right
updated_history = (new_history_left != self._history_left or new_history_width != self._history_width)
if updated_history:
self._history_left = new_history_left
self._history_width = new_history_width
new_history_width = self._scene_width - new_history_left - self._margin_right
self._history_left = new_history_left
self._history_width = new_history_width

# Calculate the bounds for each topic
self._history_bounds = {}
Expand Down Expand Up @@ -544,7 +585,7 @@ def _draw_topic_names(self, painter):
painter.setBrush(self._default_brush)
painter.setPen(self._default_pen)
painter.setFont(self._topic_font)
painter.drawText(coords[0], coords[1], text)
painter.drawText(coords[0], coords[1], self._trimmed_topic_name(text))

def _draw_time_divisions(self, painter):
"""
Expand Down Expand Up @@ -584,7 +625,7 @@ def _draw_major_divisions(self, painter, stamps, start_stamp, division):

label = self._get_label(division, stamp - start_stamp)
label_x = x + self._major_divisions_label_indent
if label_x + QFontMetrics(self._topic_font).width(label) < self.scene().width():
if label_x + self._qfont_width(label) < self.scene().width():
painter.setBrush(self._default_brush)
painter.setPen(self._default_pen)
painter.setFont(self._time_font)
Expand Down

0 comments on commit dd6f42d

Please sign in to comment.