home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / hplip / ui / scrollstatus.py < prev    next >
Encoding:
Python Source  |  2007-04-04  |  12.9 KB  |  346 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2001-2007 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Author: Don Welch
  20. #
  21.  
  22. # Local
  23. from base.g import *
  24. from base import utils
  25.  
  26. # Qt
  27. from qt import *
  28. from scrollview import ScrollView
  29.  
  30. # Std Lib
  31. import os.path, os
  32. import time
  33.  
  34. try:
  35.     import datetime
  36.     have_datetime = True
  37. except ImportError:
  38.     have_datetime = False
  39.  
  40.  
  41.  
  42. class ScrollStatusView(ScrollView):
  43.     def __init__(self,parent = None,name = None,fl = 0):
  44.         ScrollView.__init__(self,parent,name,fl)
  45.  
  46.         self.warning_pix = QPixmap(os.path.join(prop.image_dir, "warning.png"))
  47.         self.error_pix = QPixmap(os.path.join(prop.image_dir, "error.png"))
  48.         self.ok_pix = QPixmap(os.path.join(prop.image_dir, "ok.png"))
  49.         self.lowink_pix = QPixmap(os.path.join(prop.image_dir, 'inkdrop.png'))
  50.         self.lowtoner_pix = QPixmap(os.path.join(prop.image_dir, 'toner.png'))
  51.         self.busy_pix = QPixmap(os.path.join(prop.image_dir, 'busy.png'))
  52.         self.lowpaper_pix = QPixmap(os.path.join(prop.image_dir, 'paper.png'))
  53.         self.idle_pix = QPixmap(os.path.join(prop.image_dir, 'idle.png'))
  54.  
  55.         self.ScanPixmap = QPixmap(os.path.join(prop.image_dir, "scan_icon.png"))
  56.         self.PrintPixmap = QPixmap(os.path.join(prop.image_dir, "print_icon.png"))
  57.         self.SendFaxPixmap =QPixmap(os.path.join(prop.image_dir, "fax_icon.png"))
  58.         self.PhotoCardPixmap = QPixmap(os.path.join(prop.image_dir, "pcard_icon.png"))
  59.         self.MakeCopiesPixmap = QPixmap(os.path.join(prop.image_dir, "makecopies_icon.png"))
  60.  
  61.         self.STATUS_ICONS = { ERROR_STATE_CLEAR : (self.idle_pix, self.idle_pix),
  62.                               ERROR_STATE_BUSY : (self.busy_pix, self.busy_pix),
  63.                               ERROR_STATE_ERROR : (self.error_pix, self.error_pix),
  64.                               ERROR_STATE_LOW_SUPPLIES : (self.lowink_pix, self.lowtoner_pix),
  65.                               ERROR_STATE_OK : (self.ok_pix, self.ok_pix),
  66.                               ERROR_STATE_WARNING : (self.warning_pix, self.warning_pix),
  67.                               ERROR_STATE_LOW_PAPER: (self.lowpaper_pix, self.lowpaper_pix),
  68.                               ERROR_STATE_PRINTING : (self.PrintPixmap, self.PrintPixmap),
  69.                               ERROR_STATE_SCANNING : (self.ScanPixmap, self.ScanPixmap),
  70.                               ERROR_STATE_PHOTOCARD : (self.PhotoCardPixmap, self.PrintPixmap),
  71.                               ERROR_STATE_FAXING : (self.SendFaxPixmap, self.SendFaxPixmap),
  72.                               ERROR_STATE_COPYING :  (self.MakeCopiesPixmap, self.MakeCopiesPixmap),
  73.                             }
  74.  
  75.         self.unit_names = { "year" : (self.__tr("year"), self.__tr("years")),
  76.                             "month" : (self.__tr("month"), self.__tr("months")),
  77.                             "week" : (self.__tr("week"), self.__tr("weeks")),
  78.                             "day" : (self.__tr("day"), self.__tr("days")),
  79.                             "hour" : (self.__tr("hour"), self.__tr("hours")),
  80.                             "minute" : (self.__tr("minute"), self.__tr("minutes")),
  81.                             "second" : (self.__tr("second"), self.__tr("seconds")),
  82.                             }
  83.  
  84.         self.num_repr = { 1 : self.__tr("one"),
  85.                           2 : self.__tr("two"),
  86.                           3 : self.__tr("three"),
  87.                           4 : self.__tr("four"),
  88.                           5 : self.__tr("five"),
  89.                           6 : self.__tr("six"),
  90.                           7 : self.__tr("seven"),
  91.                           8 : self.__tr("eight"),
  92.                           9 : self.__tr("nine"),
  93.                           10 : self.__tr("ten"),
  94.                           11 : self.__tr("eleven"),
  95.                           12 : self.__tr("twelve")
  96.                           }
  97.  
  98.     def fillControls(self):
  99.         ScrollView.fillControls(self)
  100.         self.row = 0
  101.         for x in self.cur_device.hist:
  102.             self.addItem(x)
  103.  
  104.     def addItem(self, hist):
  105.         yr, mt, dy, hr, mi, sec, wd, yd, dst, job_id, user, ec, ess, esl = hist
  106.  
  107.         if self.row == 0:
  108.             desc = self.__tr("(most recent)")
  109.  
  110.         else:
  111.             if have_datetime:
  112.                 desc = self.getTimeDeltaDesc(hist[:9])
  113.             else:
  114.                 desc = ''
  115.         
  116.         #print repr(str(desc))
  117.         tt = QString("<b>%1 %2</b>").arg(time.strftime("%x %H:%M:%S", hist[:9])).arg(desc).stripWhiteSpace()
  118.         #tt = QString(desc)
  119.         self.addGroupHeading(str(tt), tt)
  120.         
  121.         widget = self.getWidget()
  122.         
  123.         layout38 = QGridLayout(widget,1,1,5,10,"layout38")
  124.         layout38.setColStretch(0, 1)
  125.         layout38.setColStretch(1, 10)
  126.         layout38.setColStretch(2, 2)
  127.         
  128.         spacer15 = QSpacerItem(30,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
  129.         layout38.addItem(spacer15,0,2)
  130.  
  131.         icon = QLabel(widget,"icon")
  132.         icon.setSizePolicy(QSizePolicy(QSizePolicy.Fixed,QSizePolicy.Fixed,0,0,icon.sizePolicy().hasHeightForWidth()))
  133.         icon.setMinimumSize(QSize(32,32))
  134.         icon.setMaximumSize(QSize(32,32))
  135.         #self.icon.setFrameShape(QLabel.Box)
  136.         icon.setScaledContents(1)
  137.  
  138.         layout38.addWidget(icon,0,0)
  139.  
  140.         layout11_2 = QVBoxLayout(None,0,6,"layout11_2")
  141.  
  142.         essText = QLabel(widget,"essTextLabel")
  143.         essText.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding,QSizePolicy.Preferred,0,0,essText.sizePolicy().hasHeightForWidth()))
  144.         essText.setAlignment(QLabel.WordBreak | QLabel.AlignVCenter)
  145.         essText.setFrameShape(self.frame_shape)
  146.         layout11_2.addWidget(essText)
  147.  
  148.         eslText = QLabel(widget,"eslTextLabel")
  149.         eslText.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding,QSizePolicy.Preferred,0,0,eslText.sizePolicy().hasHeightForWidth()))
  150.         eslText.setAlignment(QLabel.WordBreak | QLabel.AlignVCenter)
  151.         eslText.setFrameShape(self.frame_shape)
  152.         layout11_2.addWidget(eslText)
  153.         
  154.         layout38.addLayout(layout11_2,0,1)
  155.  
  156.         layout12_2 = QGridLayout(None,1,1,5,10,"layout12_2")
  157.  
  158.         userTextLabel = QLabel(widget,"userLabel")
  159.         layout12_2.addWidget(userTextLabel,0,1)
  160.  
  161.         jobIDTextLabel = QLabel(widget,"jobLabel")
  162.         layout12_2.addWidget(jobIDTextLabel,1,1)
  163.  
  164.         userText = QLabel(widget,"user")
  165.         layout12_2.addWidget(userText,0,2)
  166.  
  167.         codeTextLabel = QLabel(widget,"codeLabel")
  168.         layout12_2.addWidget(codeTextLabel,2,1)
  169.  
  170.         jobIDText = QLabel(widget,"job")
  171.         layout12_2.addWidget(jobIDText,1,2)
  172.  
  173.         codeText = QLabel(widget,"code")
  174.         layout12_2.addWidget(codeText,2,2)
  175.  
  176.         line6 = QFrame(self,"line6")
  177.         line6.setFrameShape(QFrame.VLine)
  178.         #self.line6.setFrameShadow(QFrame.Sunken)
  179.         #self.line6.setFrameShape(QFrame.VLine)
  180.  
  181.         layout12_2.addMultiCellWidget(line6,0,2,0,0)
  182.  
  183.         layout38.addLayout(layout12_2,0,3)
  184.  
  185.        
  186. ##        layout1 = QGridLayout(widget, 1, 1, 5, 10, "layout1")
  187. ##        spacer1 = QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
  188. ##        layout1.addItem(spacer1, 1, 2)
  189. ##
  190. ##        icon = QLabel(widget, "icon")
  191. ##        icon.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed, 0, 0,
  192. ##            icon.sizePolicy().hasHeightForWidth()))
  193. ##
  194. ##        icon.setMinimumSize(QSize(32, 32))
  195. ##        icon.setMaximumSize(QSize(32, 32))
  196. ##        icon.setScaledContents(1)
  197. ##        layout1.addWidget(icon, 0, 0)
  198. ##
  199. ##        #layout2 = QGridLayout(None, 1, 1, 0, 0, "layout2")
  200. ##        layout2 = QVBoxLayout(None, 0, 6, "layout2")
  201. ##        
  202. ##        userTextLabel = QLabel(widget, "userTextLabel")
  203. ##        layout2.addWidget(userTextLabel, 0, 1)
  204. ##
  205. ##        jobIDTextLabel = QLabel(widget, "jobIDTextLabel")
  206. ##        layout2.addWidget(jobIDTextLabel, 1, 1)
  207. ##
  208. ##        userText = QLabel(widget, "userText")
  209. ##        layout2.addWidget(userText, 0, 2)
  210. ##
  211. ##        codeTextLabel = QLabel(widget, "codeTextLabel")
  212. ##        layout2.addWidget(codeTextLabel, 2, 1)
  213. ##
  214. ##        jobIDText = QLabel(widget, "jobIDText")
  215. ##        layout2.addWidget(jobIDText, 1, 2)
  216. ##
  217. ##        codeText = QLabel(widget, "codeText")
  218. ##        layout2.addWidget(codeText, 2, 2)
  219. ##
  220. ##        layout1.addMultiCellLayout(layout2, 0, 1, 3, 3)
  221. ##        
  222. ##        layout3 = QVBoxLayout(None, 0, 6, "layout3")
  223. ##
  224. ##        essText = QLabel(widget, "essText")
  225. ##        essText.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Maximum,0,0,
  226. ##            essText.sizePolicy().hasHeightForWidth()))
  227. ##            
  228. ##        essText.setAlignment(QLabel.WordBreak) # | QLabel.AlignVCenter)
  229. ##        essText.setFrameShape(self.frame_shape)
  230. ##        
  231. ##        layout3.addWidget(essText, 0, 1)
  232. ##
  233. ##        eslText = QLabel(widget, "eslText")
  234. ##        eslText.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Maximum,0,0,
  235. ##            eslText.sizePolicy().hasHeightForWidth()))
  236. ##        eslText.setFrameShape(self.frame_shape)
  237. ##        
  238. ##        eslText.setAlignment(QLabel.WordBreak) # | QLabel.AlignVCenter)
  239. ##        layout3.addWidget(eslText, 1, 1)
  240. ##        
  241. ##        layout1.addLayout(layout3, 0, 1)
  242. ##        layout1.addLayout(layout2, 1, 1)
  243. ##
  244. ##        #titleText = QLabel(widget, "titleText")
  245. ##        #titleText.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred, 0, 0,
  246. ##        #    titleText.sizePolicy().hasHeightForWidth()))
  247. ##        
  248. ##        #layout1.addMultiCellWidget(titleText, 0, 0, 0, 2)
  249. ##
  250. ##        #line2 = QFrame(widget, "line1")
  251. ##        #line2.setFrameShape(QFrame.HLine)
  252. ##        #layout1.addMultiCellWidget(line2, 2, 2, 0, 3)
  253. ##
  254. ####        if self.row == 0:
  255. ####            desc = self.__tr("(most recent)")
  256. ####
  257. ####        else:
  258. ####            if have_datetime:
  259. ####                desc = self.getTimeDeltaDesc(hist[:9])
  260. ####            else:
  261. ####                desc = ''
  262. ##
  263. ##        #titleText.setText(QString("<b>%1 %2</b>").arg(time.strftime("%x %H:%M:%S", hist[:9])).arg(desc))
  264.         
  265.  
  266.         essText.setText(ess)
  267.         eslText.setText(esl)
  268.  
  269.         userTextLabel.setText(self.__tr("User:"))
  270.         userText.setText(user)
  271.  
  272.         jobIDTextLabel.setText(self.__tr("Job ID:"))
  273.         if job_id <= 0:
  274.             jobIDText.setText(self.__tr("n/a"))
  275.         else:
  276.             jobIDText.setText(str(job_id))
  277.  
  278.         codeTextLabel.setText(self.__tr("Code:"))
  279.         codeText.setText(str(ec))
  280.  
  281.         error_state = STATUS_TO_ERROR_STATE_MAP.get(ec, ERROR_STATE_CLEAR)
  282.  
  283.         try:
  284.             tech_type = self.cur_device.tech_type
  285.         except AttributeError:
  286.             tech_type = TECH_TYPE_NONE
  287.  
  288.         if tech_type in (TECH_TYPE_COLOR_INK, TECH_TYPE_MONO_INK):
  289.             status_pix = self.STATUS_ICONS[error_state][0] # ink
  290.         else:
  291.             status_pix = self.STATUS_ICONS[error_state][1] # laser
  292.  
  293.         if status_pix is not None:
  294.             icon.setPixmap(status_pix)
  295.  
  296.         self.row += 1
  297.         self.addWidget(widget, str(self.row))
  298.  
  299.  
  300.  
  301.     def getTimeDeltaDesc(self, past):
  302.         delta = datetime.datetime(*time.localtime()[:7]) - datetime.datetime(*past[:7])
  303.         return self.__tr("(about %1 ago)").arg(self.stringify(delta))
  304.  
  305.  
  306.     # "Nicely readable timedelta"
  307.     # Credit: Bjorn Lindqvist
  308.     # ASPN Python Recipe 498062 
  309.     # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498062
  310.     # Note: Modified from recipe
  311.     def seconds_in_units(self, seconds):
  312.         unit_limits = [("year", 31536000),
  313.                        ("month", 2592000),
  314.                        ("week", 604800),
  315.                        ("day", 86400),
  316.                        ("hour", 3600),
  317.                        ("minute", 60)]
  318.  
  319.         for unit_name, limit in unit_limits:
  320.             if seconds >= limit:
  321.                 amount = int(round(float(seconds) / limit))
  322.                 return amount, unit_name
  323.  
  324.         return seconds, "second"
  325.  
  326.     def stringify(self, td):
  327.         seconds = td.days * 3600 * 24 + td.seconds
  328.         amount, unit_name = self.seconds_in_units(seconds)
  329.  
  330.         try:
  331.             i18n_amount = self.num_repr[amount]
  332.         except KeyError:
  333.             i18n_amount = str(amount)
  334.  
  335.         if amount == 1:
  336.             i18n_unit = self.unit_names[unit_name][0]
  337.         else:
  338.             i18n_unit = self.unit_names[unit_name][1]
  339.  
  340.         return "%s %s" % (i18n_amount, i18n_unit)
  341.  
  342.  
  343.     def __tr(self,s,c = None):
  344.         return qApp.translate("DevMgr4",s,c)
  345.         
  346.