Previously, update only worked on files passed as command-line
arguments. This patch extends the feature to files opened from the menu.
- update method now is in the widget class, where it can benefit from
the openfilename variable. It also reuses the widget's reload method.
- the gobject timeout now happens whether or not a file was passed in
the command line.
Signed-off-by: José Fonseca <jose.r.fonseca@gmail.com>
... | ... |
@@ -1430,6 +1430,9 @@ class DotWidget(gtk.DrawingArea): |
1430 | 1430 |
self.connect("size-allocate", self.on_area_size_allocate) |
1431 | 1431 |
|
1432 | 1432 |
self.connect('key-press-event', self.on_key_press_event) |
1433 |
+ self.last_mtime = None |
|
1434 |
+ |
|
1435 |
+ gobject.timeout_add(1000, self.update) |
|
1433 | 1436 |
|
1434 | 1437 |
self.x, self.y = 0.0, 0.0 |
1435 | 1438 |
self.zoom_ratio = 1.0 |
... | ... |
@@ -1465,7 +1468,8 @@ class DotWidget(gtk.DrawingArea): |
1465 | 1468 |
return None |
1466 | 1469 |
return xdotcode |
1467 | 1470 |
|
1468 |
- def set_dotcode(self, dotcode, filename='<stdin>'): |
|
1471 |
+ def set_dotcode(self, dotcode, filename=None): |
|
1472 |
+ self.openfilename = None |
|
1469 | 1473 |
if isinstance(dotcode, unicode): |
1470 | 1474 |
dotcode = dotcode.encode('utf8') |
1471 | 1475 |
xdotcode = self.run_filter(dotcode) |
... | ... |
@@ -1482,6 +1486,10 @@ class DotWidget(gtk.DrawingArea): |
1482 | 1486 |
dialog.destroy() |
1483 | 1487 |
return False |
1484 | 1488 |
else: |
1489 |
+ if filename is None: |
|
1490 |
+ self.last_mtime = None |
|
1491 |
+ else: |
|
1492 |
+ self.last_mtime = os.stat(filename).st_mtime |
|
1485 | 1493 |
self.openfilename = filename |
1486 | 1494 |
return True |
1487 | 1495 |
|
... | ... |
@@ -1500,6 +1508,14 @@ class DotWidget(gtk.DrawingArea): |
1500 | 1508 |
except IOError: |
1501 | 1509 |
pass |
1502 | 1510 |
|
1511 |
+ def update(self): |
|
1512 |
+ if self.openfilename is not None: |
|
1513 |
+ current_mtime = os.stat(self.openfilename).st_mtime |
|
1514 |
+ if current_mtime != self.last_mtime: |
|
1515 |
+ self.last_mtime = current_mtime |
|
1516 |
+ self.reload() |
|
1517 |
+ return True |
|
1518 |
+ |
|
1503 | 1519 |
def do_expose_event(self, event): |
1504 | 1520 |
cr = self.window.cairo_create() |
1505 | 1521 |
|
... | ... |
@@ -1749,6 +1765,8 @@ class DotWindow(gtk.Window): |
1749 | 1765 |
</ui> |
1750 | 1766 |
''' |
1751 | 1767 |
|
1768 |
+ base_title = 'Dot Viewer' |
|
1769 |
+ |
|
1752 | 1770 |
def __init__(self): |
1753 | 1771 |
gtk.Window.__init__(self) |
1754 | 1772 |
|
... | ... |
@@ -1756,7 +1774,7 @@ class DotWindow(gtk.Window): |
1756 | 1774 |
|
1757 | 1775 |
window = self |
1758 | 1776 |
|
1759 |
- window.set_title('Dot Viewer') |
|
1777 |
+ window.set_title(self.base_title) |
|
1760 | 1778 |
window.set_default_size(512, 512) |
1761 | 1779 |
vbox = gtk.VBox() |
1762 | 1780 |
window.add(vbox) |
... | ... |
@@ -1800,30 +1818,24 @@ class DotWindow(gtk.Window): |
1800 | 1818 |
|
1801 | 1819 |
self.show_all() |
1802 | 1820 |
|
1803 |
- def update(self, filename): |
|
1804 |
- import os |
|
1805 |
- if not hasattr(self, "last_mtime"): |
|
1806 |
- self.last_mtime = None |
|
1807 |
- |
|
1808 |
- current_mtime = os.stat(filename).st_mtime |
|
1809 |
- if current_mtime != self.last_mtime: |
|
1810 |
- self.last_mtime = current_mtime |
|
1811 |
- self.open_file(filename) |
|
1812 |
- |
|
1813 |
- return True |
|
1814 |
- |
|
1815 | 1821 |
def set_filter(self, filter): |
1816 | 1822 |
self.widget.set_filter(filter) |
1817 | 1823 |
|
1818 |
- def set_dotcode(self, dotcode, filename='<stdin>'): |
|
1824 |
+ def set_dotcode(self, dotcode, filename=None): |
|
1819 | 1825 |
if self.widget.set_dotcode(dotcode, filename): |
1820 |
- self.set_title(os.path.basename(filename) + ' - Dot Viewer') |
|
1826 |
+ self.update_title(filename) |
|
1821 | 1827 |
self.widget.zoom_to_fit() |
1822 | 1828 |
|
1823 |
- def set_xdotcode(self, xdotcode, filename='<stdin>'): |
|
1829 |
+ def set_xdotcode(self, xdotcode, filename=None): |
|
1824 | 1830 |
if self.widget.set_xdotcode(xdotcode): |
1825 |
- self.set_title(os.path.basename(filename) + ' - Dot Viewer') |
|
1831 |
+ self.update_title(filename) |
|
1826 | 1832 |
self.widget.zoom_to_fit() |
1833 |
+ |
|
1834 |
+ def update_title(self, filename=None): |
|
1835 |
+ if filename is None: |
|
1836 |
+ self.set_title(self.base_title) |
|
1837 |
+ else: |
|
1838 |
+ self.set_title(os.path.basename(filename) + ' - ' + self.base_title) |
|
1827 | 1839 |
|
1828 | 1840 |
def open_file(self, filename): |
1829 | 1841 |
try: |
... | ... |
@@ -1834,7 +1846,7 @@ class DotWindow(gtk.Window): |
1834 | 1846 |
dlg = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, |
1835 | 1847 |
message_format=str(ex), |
1836 | 1848 |
buttons=gtk.BUTTONS_OK) |
1837 |
- dlg.set_title('Dot Viewer') |
|
1849 |
+ dlg.set_title(self.base_title) |
|
1838 | 1850 |
dlg.run() |
1839 | 1851 |
dlg.destroy() |
1840 | 1852 |
|
... | ... |
@@ -1893,7 +1905,6 @@ def main(): |
1893 | 1905 |
win.set_dotcode(sys.stdin.read()) |
1894 | 1906 |
else: |
1895 | 1907 |
win.open_file(args[0]) |
1896 |
- gobject.timeout_add(1000, win.update, args[0]) |
|
1897 | 1908 |
gtk.main() |
1898 | 1909 |
|
1899 | 1910 |
|