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 / scrollsupplies.py < prev    next >
Encoding:
Python Source  |  2007-04-04  |  12.4 KB  |  339 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. from prnt import cups
  26.  
  27. # Qt
  28. from qt import *
  29. from scrollview import ScrollView
  30.  
  31. # Std Lib
  32. import os.path, os
  33.  
  34.  
  35.  
  36. class ScrollSuppliesView(ScrollView):
  37.     def __init__(self,parent = None,name = None,fl = 0):
  38.         ScrollView.__init__(self,parent,name,fl)
  39.  
  40.         self.pix_battery = QPixmap(os.path.join(prop.image_dir, 'icon_battery.png'))
  41.  
  42.         yellow = "#ffff00"
  43.         light_yellow = "#ffffcc"
  44.         cyan = "#00ffff"
  45.         light_cyan = "#ccffff"
  46.         magenta = "#ff00ff"
  47.         light_magenta = "#ffccff"
  48.         black = "#000000"
  49.         blue = "#0000ff"
  50.         dark_grey = "#808080"
  51.         light_grey = "#c0c0c0"
  52.         
  53.         self.TYPE_TO_PIX_MAP = {
  54.                                AGENT_TYPE_UNSPECIFIED : [black],
  55.                                AGENT_TYPE_BLACK: [black],
  56.                                AGENT_TYPE_CMY: [cyan, magenta, yellow],
  57.                                AGENT_TYPE_KCM: [light_cyan, light_magenta, light_yellow],
  58.                                AGENT_TYPE_GGK: [dark_grey],
  59.                                AGENT_TYPE_YELLOW: [yellow],
  60.                                AGENT_TYPE_MAGENTA: [magenta],
  61.                                AGENT_TYPE_CYAN : [cyan],
  62.                                AGENT_TYPE_CYAN_LOW: [light_cyan],
  63.                                AGENT_TYPE_YELLOW_LOW: [light_yellow],
  64.                                AGENT_TYPE_MAGENTA_LOW: [light_magenta],
  65.                                AGENT_TYPE_BLUE: [blue],
  66.                                AGENT_TYPE_KCMY_CM: [yellow, cyan, magenta],
  67.                                AGENT_TYPE_LC_LM: [light_cyan, light_magenta],
  68.                                #AGENT_TYPE_Y_M: [yellow, magenta],
  69.                                #AGENT_TYPE_C_K: [black, cyan],
  70.                                AGENT_TYPE_LG_PK: [light_grey, dark_grey],
  71.                                AGENT_TYPE_LG: [light_grey],
  72.                                AGENT_TYPE_G: [dark_grey],
  73.                                AGENT_TYPE_PG: [light_grey],
  74.                                AGENT_TYPE_C_M: [cyan, magenta],
  75.                                AGENT_TYPE_K_Y: [black, yellow],
  76.                                }
  77.  
  78.     def fillControls(self):
  79.         ScrollView.fillControls(self)
  80.         
  81.         if self.cur_device is not None and \
  82.             self.cur_device.supported and \
  83.             self.cur_device.status_type != STATUS_TYPE_NONE and \
  84.             self.cur_device.device_state != DEVICE_STATE_NOT_FOUND:
  85.  
  86.             try:
  87.                 self.cur_device.sorted_supplies
  88.             except AttributeError:                
  89.                 self.cur_device.sorted_supplies = []
  90.  
  91.             if not self.cur_device.sorted_supplies:
  92.                 a = 1
  93.                 while True:
  94.                     try:
  95.                         agent_type = int(self.cur_device.dq['agent%d-type' % a])
  96.                         agent_kind = int(self.cur_device.dq['agent%d-kind' % a])
  97.                     except KeyError:
  98.                         break
  99.                     else:
  100.                         self.cur_device.sorted_supplies.append((a, agent_kind, agent_type))
  101.  
  102.                     a += 1
  103.  
  104.                 self.cur_device.sorted_supplies.sort(lambda x, y: cmp(x[2], y[2]) or cmp(x[1], y[1]))
  105.  
  106.             for x in self.cur_device.sorted_supplies:
  107.                 a, agent_kind, agent_type = x
  108.                 agent_level = int(self.cur_device.dq['agent%d-level' % a])
  109.                 agent_sku = str(self.cur_device.dq['agent%d-sku' % a])
  110.                 agent_desc = self.cur_device.dq['agent%d-desc' % a]
  111.                 agent_health_desc = self.cur_device.dq['agent%d-health-desc' % a]
  112.  
  113.                 self.addItem("agent %d" % a, "<b>"+agent_desc+"</b>",
  114.                                           agent_sku, agent_health_desc, 
  115.                                           agent_kind, agent_type, agent_level)
  116.                                           
  117.  
  118.         else:
  119.             if not self.cur_device.supported:
  120.                 
  121.                 self.addGroupHeading("not_supported", self.__tr("ERROR: Device not supported."))
  122.             
  123.             elif self.cur_device.status_type == STATUS_TYPE_NONE:
  124.                 
  125.                 self.addGroupHeading("not_found", self.__tr("ERROR: Supplies status is not supported on this device."))
  126.                 
  127.             else:
  128.                 self.addGroupHeading("not_found", self.__tr("ERROR: Device not found. Please check connection and power-on device."))
  129.  
  130.                 
  131.     def getIcon(self, agent_kind, agent_type):
  132.         if agent_kind in (AGENT_KIND_SUPPLY,
  133.                           AGENT_KIND_HEAD,
  134.                           AGENT_KIND_HEAD_AND_SUPPLY,
  135.                           AGENT_KIND_TONER_CARTRIDGE):
  136.  
  137.             map = self.TYPE_TO_PIX_MAP[agent_type]
  138.             
  139.             if isinstance(map, list):
  140.                 map_len = len(map)
  141.                 pix = QPixmap(32, 32) #, -1, QPixmap.DefaultOptim)
  142.                 pix.fill(qApp.palette().color(QPalette.Active, QColorGroup.Background))
  143.                 p = QPainter()
  144.                 p.begin(pix)
  145.                 p.setBackgroundMode(Qt.OpaqueMode)
  146.                 
  147.                 if map_len == 1:
  148.                     p.setPen(QColor(map[0]))
  149.                     p.setBrush(QBrush(QColor(map[0]), Qt.SolidPattern))
  150.                     p.drawPie(8, 8, 16, 16, 0, 5760)
  151.                 
  152.                 elif map_len == 2:
  153.                     p.setPen(QColor(map[0]))
  154.                     p.setBrush(QBrush(QColor(map[0]), Qt.SolidPattern))
  155.                     p.drawPie(4, 8, 16, 16, 0, 5760)
  156.                     
  157.                     p.setPen(QColor(map[1]))
  158.                     p.setBrush(QBrush(QColor(map[1]), Qt.SolidPattern))
  159.                     p.drawPie(12, 8, 16, 16, 0, 5760)
  160.                 
  161.                 elif map_len == 3:
  162.                     p.setPen(QColor(map[2]))
  163.                     p.setBrush(QBrush(QColor(map[2]), Qt.SolidPattern))
  164.                     p.drawPie(12, 12, 16, 16, 0, 5760)
  165.                 
  166.                     p.setPen(QColor(map[1]))
  167.                     p.setBrush(QBrush(QColor(map[1]), Qt.SolidPattern))
  168.                     p.drawPie(4, 12, 16, 16, 0, 5760)
  169.                     
  170.                     p.setPen(QColor(map[0]))
  171.                     p.setBrush(QBrush(QColor(map[0]), Qt.SolidPattern))
  172.                     p.drawPie(8, 4, 16, 16, 0, 5760)
  173.                 
  174.                 p.end()
  175.                 return pix
  176.             
  177.             else:
  178.                 return map
  179.  
  180.         elif agent_kind == AGENT_KIND_INT_BATTERY:
  181.                 return self.pix_battery
  182.  
  183.  
  184.     def createBarGraph(self, percent, agent_type, w=100, h=18):
  185.         fw = w/100*percent
  186.         px = QPixmap(w, h)
  187.         px.fill(qApp.palette().color(QPalette.Active, QColorGroup.Background))
  188.         
  189.         pp = QPainter(px)
  190.         pp.setPen(Qt.black)
  191.         pp.setBackgroundColor(qApp.palette().color(QPalette.Active, QColorGroup.Base))
  192.  
  193.         map = self.TYPE_TO_PIX_MAP[agent_type]
  194.         map_len = len(map)
  195.         
  196.         if map_len == 1 or map_len > 3:
  197.             pp.fillRect(0, 0, fw, h, QBrush(QColor(map[0])))
  198.  
  199.         elif map_len == 2:
  200.             h2 = h / 2
  201.             pp.fillRect(0, 0, fw, h2, QBrush(QColor(map[0])))
  202.             pp.fillRect(0, h2, fw, h, QBrush(QColor(map[1])))
  203.             
  204.         elif map_len == 3:
  205.             h3 = h / 3
  206.             h23 = 2 * h3
  207.             pp.fillRect(0, 0, fw, h3, QBrush(QColor(map[0])))
  208.             pp.fillRect(0, h3, fw, h23, QBrush(QColor(map[1])))
  209.             pp.fillRect(0, h23, fw, h, QBrush(QColor(map[2])))
  210.  
  211.         # draw black frame
  212.         pp.drawRect(0, 0, w, h)
  213.  
  214.         if percent > 75 and agent_type in \
  215.           (AGENT_TYPE_BLACK, AGENT_TYPE_UNSPECIFIED, AGENT_TYPE_BLUE):
  216.             pp.setPen(Qt.white)
  217.  
  218.         # 75% ticks
  219.         w1 = 3 * w / 4
  220.         h6 = h / 6
  221.         pp.drawLine(w1, 0, w1, h6)
  222.         pp.drawLine(w1, h, w1, h-h6)
  223.  
  224.         if percent > 50 and agent_type in \
  225.           (AGENT_TYPE_BLACK, AGENT_TYPE_UNSPECIFIED, AGENT_TYPE_BLUE):
  226.             pp.setPen(Qt.white)
  227.  
  228.         # 50% ticks
  229.         w2 = w / 2
  230.         h4 = h / 4
  231.         pp.drawLine(w2, 0, w2, h4)
  232.         pp.drawLine(w2, h, w2, h-h4)
  233.  
  234.         if percent > 25 and agent_type in \
  235.           (AGENT_TYPE_BLACK, AGENT_TYPE_UNSPECIFIED, AGENT_TYPE_BLUE):
  236.             pp.setPen(Qt.white)
  237.  
  238.         # 25% ticks
  239.         w4 = w / 4
  240.         pp.drawLine(w4, 0, w4, h6)
  241.         pp.drawLine(w4, h, w4, h-h6)
  242.  
  243.         return px   
  244.  
  245.  
  246.     def addItem(self, name, title_text, part_num_text, status_text, 
  247.                 agent_kind, agent_type, percent):
  248.  
  249.         self.addGroupHeading(title_text, title_text)
  250.         
  251.         widget = self.getWidget()
  252.         layout1 = QGridLayout(widget,1,1,5,10,"layout1")
  253.  
  254.         spacer1 = QSpacerItem(20,20,QSizePolicy.Preferred,QSizePolicy.Minimum)
  255.         layout1.addItem(spacer1,0,3)
  256.  
  257.         barGraph = QLabel(widget,"barGraph")
  258.         barGraph.setSizePolicy(QSizePolicy(QSizePolicy.Fixed,QSizePolicy.Fixed,0,0,
  259.             barGraph.sizePolicy().hasHeightForWidth()))
  260.  
  261.         barGraph.setMinimumSize(QSize(100,18))
  262.         barGraph.setMaximumSize(QSize(100,18))
  263.         barGraph.setScaledContents(1)
  264.         layout1.addMultiCellWidget(barGraph,0,0,4,5)
  265.  
  266.         #titleText = QLabel(widget,"titleText")
  267.         #layout1.addMultiCellWidget(titleText,0,0,0,2)
  268.  
  269.         #line1 = QFrame(widget,"line1")
  270.         #line1.setFrameShape(QFrame.HLine)
  271.         #layout1.addMultiCellWidget(line1,2,2,0,5)
  272.  
  273.         spacer2 = QSpacerItem(20,20,QSizePolicy.Preferred,QSizePolicy.Minimum)
  274.         layout1.addMultiCell(spacer2,1,1,2,4)
  275.  
  276.         statusText = QLabel(widget,"statusText")
  277.         statusText.setFrameShape(self.frame_shape)
  278.         layout1.addWidget(statusText,1,5)
  279.  
  280.         icon = QLabel(widget,"icon")
  281.         icon.setSizePolicy(QSizePolicy(QSizePolicy.Fixed,QSizePolicy.Fixed,0,0,
  282.             icon.sizePolicy().hasHeightForWidth()))
  283.  
  284.         icon.setMinimumSize(QSize(32,32))
  285.         icon.setMaximumSize(QSize(32,32))
  286.         icon.setScaledContents(1)
  287.         layout1.addWidget(icon,0,0)
  288.  
  289.         partNumText = QLabel(widget,"partNumText")
  290.         partNumText.setFrameShape(self.frame_shape)
  291.         partNumText.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding,QSizePolicy.Preferred,0,0,
  292.             partNumText.sizePolicy().hasHeightForWidth()))
  293.  
  294.         partNumText.setAlignment(QLabel.WordBreak | QLabel.AlignVCenter)
  295.         layout1.addWidget(partNumText,0,1)                
  296.  
  297.         #titleText.setText(title_text)
  298.         
  299.         if part_num_text:
  300.             partNumText.setText(self.__tr("Part No. %1").arg(part_num_text))
  301.         
  302.         statusText.setText(status_text)
  303.  
  304.         # Bar graph level
  305.         if agent_kind in (AGENT_KIND_SUPPLY,
  306.                           #AGENT_KIND_HEAD,
  307.                           AGENT_KIND_HEAD_AND_SUPPLY,
  308.                           AGENT_KIND_TONER_CARTRIDGE,
  309.                           AGENT_KIND_MAINT_KIT,
  310.                           AGENT_KIND_ADF_KIT,
  311.                           AGENT_KIND_INT_BATTERY,
  312.                           AGENT_KIND_DRUM_KIT,
  313.                           ):
  314.  
  315.             barGraph.setPixmap(self.createBarGraph(percent, agent_type))
  316.  
  317.         # Color icon
  318.         if agent_kind in (AGENT_KIND_SUPPLY,
  319.                           AGENT_KIND_HEAD,
  320.                           AGENT_KIND_HEAD_AND_SUPPLY,
  321.                           AGENT_KIND_TONER_CARTRIDGE,
  322.                           #AGENT_KIND_MAINT_KIT,
  323.                           #AGENT_KIND_ADF_KIT,
  324.                           AGENT_KIND_INT_BATTERY,
  325.                           #AGENT_KIND_DRUM_KIT,
  326.                           ):
  327.  
  328.             pix = self.getIcon(agent_kind, agent_type)
  329.  
  330.             if pix is not None:
  331.                 icon.setPixmap(pix)
  332.  
  333.         self.addWidget(widget, name)
  334.  
  335.  
  336.     def __tr(self,s,c = None):
  337.         return qApp.translate("DevMgr4",s,c)
  338.  
  339.