fix a problem when dragging an item and dropping it into the item list
[qtodo.git] / qtodo.py
blob6c91c1149626e312275b491dd1802cbdf4fb734e
1 #!/usr/bin/env python
3 from PyQt4 import Qt, QtCore, QtGui
4 import sys
6 sys.path.append('./src')
8 from qtodoIO import *
9 from mainwin import *
10 from AddCatDlg import Ui_D_Add
11 from ToDoCategoryList import *
12 from ToDoItemList import *
15 class Trash(QtGui.QLabel):
16 def __init__(self, DbEngine, parent=None):
17 QtGui.QLabel.__init__(self, parent)
18 self.setAcceptDrops(True)
19 self.setPixmap(QtGui.QPixmap("pixmaps/eraser.png"))
20 self.setSizePolicy(QtGui.QSizePolicy.Minimum,QtGui.QSizePolicy.Minimum)
21 self.adjustSize()
22 self.DbEngine = DbEngine
24 def dropEvent(self, event):
25 if event.mimeData().hasFormat("application/x-qtodo"):
26 pieceData = event.mimeData().data("application/x-qtodo")
27 dataStream = QtCore.QDataStream(pieceData, QtCore.QIODevice.ReadOnly)
28 oid = QtCore.QString()
29 item_type = QtCore.QString()
30 dataStream >> oid >> item_type
31 event.setDropAction(QtCore.Qt.MoveAction)
32 if item_type == "list":
33 # Delete the category from database
34 res = self.DbEngine.DeleteList(oid)
35 if res == -1:
36 res = QtGui.QMessageBox.critical(None, "Error",
37 "There are some items still assigned to this category",
38 QtGui.QMessageBox.Ok )
39 self.setFrameShape(QtGui.QFrame.NoFrame)
40 self.setFrameShadow(QtGui.QFrame.Plain)
41 event.ignore()
42 else:
43 event.accept()
44 if item_type == "item":
45 # Delete the category from database
46 res = self.DbEngine.DeleteItem(oid)
47 if res == -1:
48 res = QtGui.QMessageBox.critical(None, "Error",
49 "The item is still open",
50 QtGui.QMessageBox.Ok )
51 self.setFrameShape(QtGui.QFrame.NoFrame)
52 self.setFrameShadow(QtGui.QFrame.Plain)
53 event.ignore()
54 else:
55 event.accept()
56 else:
57 event.ignore()
58 self.setFrameShape(QtGui.QFrame.NoFrame)
59 self.setFrameShadow(QtGui.QFrame.Plain)
61 def dragEnterEvent(self, event):
62 self.setFrameShape(QtGui.QFrame.Panel)
63 self.setFrameShadow(QtGui.QFrame.Raised)
64 event.accept()
66 def dragLeaveEvent(self, event):
67 self.setFrameShape(QtGui.QFrame.NoFrame)
68 self.setFrameShadow(QtGui.QFrame.Plain)
70 class AddList(QtGui.QLabel):
71 def __init__(self, DbEngine, parent=None):
72 QtGui.QLabel.__init__(self, parent)
73 self.setAcceptDrops(True)
74 self.setPixmap(QtGui.QPixmap("pixmaps/eraser.png"))
75 self.DbEngine = DbEngine
77 def dropEvent(self, event):
78 if event.mimeData().hasFormat("application/x-qtodo"):
79 pieceData = event.mimeData().data("application/x-qtodo")
80 dataStream = QtCore.QDataStream(pieceData, QtCore.QIODevice.ReadOnly)
82 oid_cat = QtCore.QString()
83 id_cat = QtCore.QString()
85 dataStream >> oid_cat >> id_cat
86 event.setDropAction(QtCore.Qt.MoveAction)
87 event.accept()
88 # Delete the category from database
89 self.DbEngine.DeleteList(oid_cat)
91 def dragEnterEvent(self, event):
92 event.accept()
94 class QTodo(Ui_MainWindow):
95 def __init__(self):
96 self.IOEngine = QTodoIO()
98 def Finalize(self):
99 QtCore.QObject.connect(ui.action_Quit, QtCore.SIGNAL("triggered()"), self.QuitApp)
100 QtCore.QObject.connect(ui.action_About, QtCore.SIGNAL("triggered()"), self.About)
101 QtCore.QObject.connect(ui.action_Documentation, QtCore.SIGNAL("triggered()"), self.Documentation)
103 # set the trash zone
104 ui.trash = Trash(self.IOEngine)
105 ui.actions_box.layout().addWidget(ui.trash, 0,0)
106 # set the todo lists area
107 ui.todo_lists = TodoLists()
108 ui.lists_box.layout().addWidget(ui.todo_lists, 1, 0)
110 ui.todo_items = ToDoItemList(self.IOEngine)
111 ui.items_box.layout().addWidget(ui.todo_items, 1, 0)
112 activeItems = self.IOEngine.CountActive()
113 closeItems = self.IOEngine.CountClosed()
114 ui.L_ActiveItem.setText(str(activeItems))
115 ui.L_CloseItem.setText(str(closeItems))
117 overdueItems = self.IOEngine.CountOverdue()
118 if overdueItems > 0:
119 f = ui.L_OverdueItem.palette()
120 f.setColor(QtGui.QPalette.Foreground,QtGui.QColor(255, 0, 0))
121 ui.L_OverdueItem.setPalette(f)
122 ui.L_OverdueItem.setText(str(overdueItems))
125 def LoadData(self):
126 categories = self.IOEngine.GetCategories()
127 for rec in categories:
128 ui.todo_lists.addList(rec["oid"], rec["id_cat"])
129 items = self.IOEngine.GetItems()
131 for rec in items:
132 ui.todo_items.addItem(rec)
134 def QuitApp(self):
135 app.quit()
138 def AddCategory(self):
139 AddCatDlg = QtGui.QDialog()
140 AddCat = Ui_D_Add()
141 AddCat.setupUi(AddCatDlg)
143 if AddCatDlg.exec_():
144 cat = AddCat.LE_cat_name.text()
145 if str(cat) == '':
146 return
147 desc = AddCat.LE_cat_desc.text()
148 it = QtGui.QListWidgetItem(QtGui.QIcon("pixmaps/todo_list.png"),cat)
149 it.setTextAlignment(QtCore.Qt.AlignVCenter)
150 ui.todo_lists.insertItem(0,it)
151 Data = {"id_category" : str(cat),
152 "ds_category" : str(desc) }
153 self.IOEngine.InsertCategory(Data)
155 def DeleteCategory(self):
156 print "Here"
158 def EditCategory(self):
159 pass
161 def About(self):
162 pass
164 def Documentation(self):
165 pass
167 app = QtGui.QApplication(sys.argv)
168 window = QtGui.QMainWindow()
169 ui = QTodo()
170 ui.setupUi(window)
172 #connect the menu items
173 ui.Finalize()
175 #Load the data present
176 ui.LoadData()
178 window.show()
179 sys.exit(app.exec_())