~feng-kylin/youker-assistant/youker-assistant

« back to all changes in this revision

Viewing changes to component/cardwidget.py

  • Committer: kobe
  • Date: 2015-02-13 07:37:10 UTC
  • Revision ID: xiangli@ubuntukylin.com-20150213073710-0jyp02ilyi5njj10
Qt Version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
### BEGIN LICENSE
 
5
 
 
6
# Copyright (C) 2013 National University of Defense Technology(NUDT) & Kylin Ltd
 
7
 
 
8
# Author:
 
9
#     Shine Huang<shenghuang@ubuntukylin.com>
 
10
# Maintainer:
 
11
#     Shine Huang<shenghuang@ubuntukylin.com>
 
12
 
 
13
# This program is free software: you can redistribute it and/or modify it
 
14
# under the terms of the GNU General Public License version 3, as published
 
15
# by the Free Software Foundation.
 
16
#
 
17
# This program is distributed in the hope that it will be useful, but
 
18
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
19
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
20
# PURPOSE.  See the GNU General Public License for more details.
 
21
#
 
22
# You should have received a copy of the GNU General Public License along
 
23
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
24
 
 
25
 
 
26
from PyQt4.QtGui import *
 
27
from PyQt4.QtCore import *
 
28
import sip
 
29
 
 
30
from models.globals import Globals
 
31
 
 
32
 
 
33
class CardWidget(QWidget):
 
34
 
 
35
    cardcount = 0
 
36
    number_per_row = -1
 
37
    itemwidth = -1
 
38
    itemheight = -1
 
39
 
 
40
    def __init__(self, itemwidth, itemheight, cardspacing, parent=None):
 
41
        QWidget.__init__(self, parent)
 
42
 
 
43
        self.itemwidth = itemwidth
 
44
        self.itemheight = itemheight
 
45
        self.cardspacing = cardspacing
 
46
 
 
47
        self.scrollArea = QScrollArea(self)
 
48
        self.cardPanel = QWidget()
 
49
        self.scrollArea.setWidget(self.cardPanel)
 
50
        self.scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
 
51
        self.scrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
 
52
 
 
53
        self.verticalScrollBar().setStyleSheet("QScrollBar:vertical{margin:0px 0px 0px 0px;background-color:rgb(255,255,255,100);border:0px;width:6px;}\
 
54
             QScrollBar::sub-line:vertical{subcontrol-origin:margin;border:1px solid red;height:13px}\
 
55
             QScrollBar::up-arrow:vertical{subcontrol-origin:margin;background-color:blue;height:13px}\
 
56
             QScrollBar::sub-page:vertical{background-color:#EEEDF0;}\
 
57
             QScrollBar::handle:vertical{background-color:#D1D0D2;width:6px;} QScrollBar::handle:vertical:hover{background-color:#14ACF5;width:6px;}  QScrollBar::handle:vertical:pressed{background-color:#0B95D7;width:6px;}\
 
58
             QScrollBar::add-page:vertical{background-color:#EEEDF0;}\
 
59
             QScrollBar::down-arrow:vertical{background-color:yellow;}\
 
60
             QScrollBar::add-line:vertical{subcontrol-origin:margin;border:1px solid green;height:13px}")
 
61
 
 
62
    # calculate card number in one page in current screen resolution
 
63
    def calculate_software_step_num(self):
 
64
        number_per_row = (self.width() + self.cardspacing) / (self.itemwidth + self.cardspacing)
 
65
        number_per_column = (self.height() + self.cardspacing) / (self.itemheight + self.cardspacing)
 
66
        Globals.SOFTWARE_STEP_NUM = number_per_row * number_per_column + number_per_row
 
67
        # print "re calculate SOFTWARE_STEP_NUM == ", Globals.SOFTWARE_STEP_NUM
 
68
 
 
69
    # calculate data
 
70
    def calculate_data(self):
 
71
        # the 'QScrollArea' inside area is 1px smaller than 'QWidget', fix it.
 
72
        self.scrollArea.setGeometry(-1, -1, self.width() + 2, self.height() + 2)
 
73
        self.cardPanel.setGeometry(0, 0, self.width(), self.height())
 
74
 
 
75
        self.number_per_row = (self.width() + self.cardspacing) / (self.itemwidth + self.cardspacing)
 
76
 
 
77
    def reload_cards(self):
 
78
        self.calculate_data()
 
79
 
 
80
        cards = self.cardPanel.children()
 
81
        self.cardcount = 0
 
82
 
 
83
        for i in range(self.count()):
 
84
            self.add_card(cards[i])
 
85
 
 
86
        self.cardcount = self.count()
 
87
 
 
88
    def add_card(self, card):
 
89
        x = int(self.cardcount % self.number_per_row) * (self.itemwidth + self.cardspacing)
 
90
        y = int(self.cardcount / self.number_per_row) * (self.itemheight + self.cardspacing)
 
91
 
 
92
        # resize the widget to adapt items
 
93
        nowHeight = y + self.itemheight
 
94
        if(nowHeight >= self.cardPanel.height()):
 
95
            self.cardPanel.resize(self.cardPanel.width(), nowHeight)
 
96
 
 
97
        card.move(x, y)
 
98
        self.cardcount = self.cardcount + 1
 
99
 
 
100
    def verticalScrollBar(self):
 
101
        return self.scrollArea.verticalScrollBar()
 
102
 
 
103
    def scrollToTop(self):
 
104
        vsb = self.scrollArea.verticalScrollBar()
 
105
        vsb.setValue(0)
 
106
 
 
107
    # count the cards
 
108
    def count(self):
 
109
        cards = self.cardPanel.children()
 
110
        return len(cards)
 
111
 
 
112
    # remove all item
 
113
    def clear(self):
 
114
        cards = self.cardPanel.children()
 
115
        for card in cards:
 
116
            sip.delete(card)
 
117
 
 
118
        self.cardPanel.setGeometry(0, 0, self.width(), self.height())
 
119
        self.cardcount = 0
 
 
b'\\ No newline at end of file'