Advertisement
Guest User

Untitled

a guest
May 4th, 2011
5,523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.99 KB | None | 0 0
  1. #!/bin/env python
  2. # coding: utf-8
  3.  
  4. from PyQt4.QtCore import *
  5. from PyQt4.QtGui import *
  6. import sys
  7.  
  8. class MainWindow(QMainWindow):
  9.     def __init__(self, parent=None):
  10.         super(MainWindow, self).__init__(parent)
  11.         self.filename = None
  12.         self.dirty = False
  13.         self.listWidget = QListWidget()
  14.         self.setCentralWidget(self.listWidget)
  15.         fileNewAction = self.createAction("&New...", self.fileNew,
  16.             QKeySequence.New, "filenew")
  17.         fileOpenAction = self.createAction("&Open...", self.fileOpen,
  18.             QKeySequence.Open, "fileopen")
  19.         fileSaveAction = self.createAction("&Save", self.fileSave,
  20.             QKeySequence.Save, "filesave")
  21.         fileMenu = self.menuBar().addMenu("&File")
  22.         fileMenu.addAction(fileNewAction)
  23.         fileMenu.addAction(fileOpenAction)
  24.         fileMenu.addAction(fileSaveAction)
  25.         fileToolbar = self.addToolBar("File")
  26.         fileToolbar.addAction(fileNewAction)
  27.         fileToolbar.addAction(fileOpenAction)
  28.         fileToolbar.addAction(fileSaveAction)
  29.         self.statusBar().showMessage("Ready...", 5000)
  30.         self.setWindowTitle("ListKeeper - Unnamed List")
  31.        
  32.     def editAdd(self):
  33.         form = AddForm(self)
  34.         if form.exec_():
  35.             if form.addAtEnd:
  36.                 self.listWidget.addItem(form.text)
  37.             else:
  38.                 self.listWidget.insertItem(0, form.text)
  39.             self.dirty = True
  40.            
  41.     class AddForm(QDialog):
  42.         def __init__(self, parent=None):
  43.             super(AddForm, self).__init__(parent)
  44.             self.textLineEdit = QLineEdit()
  45.             label = QLabel("&Add Text:")
  46.             label.setBuddy(self.textLineEdit)
  47.             self.addAtStartRadioButton = QRadioButton("Add at &Start")
  48.             self.addAtEndRadioButton = QRadioButton("Add at &End")
  49.             self.addAtEndRadioButton.setChecked(True)
  50.             buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
  51.             topLayout = QHBoxLayout()
  52.             topLayout.addWidget(label)
  53.             topLayout.addWidget(self.textLineEdit)
  54.             middleLayout = QHBoxLayout()
  55.             middleLayout.addWidget(self.addAtStartRadioButton)
  56.             middleLayout.addWidget(self.addAtEndRadioButton)
  57.             layout = QVBoxLayout()
  58.             layout.addLayout(topLayout)
  59.             layout.addLayout(middleLayout)
  60.             layout.addStretch()
  61.             layout.addWidget(buttonBox)
  62.             self.setLayout(layout)            
  63.             buttonBox.accepted.connect(self.accept)
  64.             buttonBox.rejected.connect(self.reject)
  65.             self.setWindowTitle("ListKeeper - Add")
  66.            
  67.     def accept(self):
  68.         self.text = self.textLineEdit.text()
  69.         if self.text.isEmpty():
  70.             super(AddForm, self).reject()
  71.         else:
  72.             self.addAtEnd = self.addAtEndRadioButton.isChecked()
  73.             super(AddForm, self).accept()            
  74.            
  75. class Squiggly(QWidget):
  76.     textChanged = pyqtSignal(str)
  77.     def __init__(self, parent=None):
  78.         super(Squiggly, self).__init__(parent)
  79.         font = self.font()
  80.         font.setPointSize(font.pointSize() + 80)
  81.         self.setFont(font)
  82.         mstring = 'I rule'
  83.         self.text = QString(mstring)
  84.         self.step = 0;
  85.         self.setWindowTitle(mstring)
  86.         self.resize(500, 360)
  87.         self.timer = QBasicTimer()
  88.         self.timer.start(20, self)            
  89.  
  90.     def timerEvent(self, event):
  91.         if (event.timerId() == self.timer.timerId()):
  92.             self.step += 1
  93.             self.update()
  94.         else:
  95.             super(Squiggly, self).timerEvent(event)
  96.            
  97.     def paintEvent(self, event):
  98.         sines = (0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38)
  99.         fm = QFontMetrics(self.font())
  100.         x = (self.width() - fm.width(self.text)) / 2
  101.         y = (self.height() + fm.ascent() - fm.descent()) / 2
  102.         color = QColor()
  103.         painter = QPainter(self)
  104.         for i in range(self.text.size()):
  105.             index = (self.step + i) % 16
  106.             color.setHsv((15 - index) * 16, 255, 191)
  107.             painter.setPen(color)
  108.             painter.drawText(x, y - ((sines[index] * fm.height()) / 400), self.text[i])
  109.             x += fm.width(self.text[i])  
  110.            
  111.     def keyPressEvent(self, event):
  112.         if event.key() in (Qt.Key_Q, Qt.Key_X, Qt.Key_Escape):
  113.             self.close()
  114.         else:
  115.             super(Squiggly, self).keyPressEvent(event)
  116.            
  117.     def mousePressEvent(self, event):
  118.         text, ok = QInputDialog.getText(self, "Squiggly - Set Text", "Text:",
  119.             QLineEdit.Normal, self.text)
  120.         if ok and not text.isEmpty():
  121.             self.text = text
  122.             self.update()
  123.             self.textChanged.emit(self.text)            
  124.            
  125. if __name__ == "__main__":
  126.     app = QApplication(sys.argv)
  127.     squiggly = Squiggly()
  128.     squiggly.show();
  129.     app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement