Exemplo n.º 1
0
    def __init__(self, note, *args, **kwargs):
        QMainWindow.__init__(self, *args, **kwargs)
        # Configure logger.
        self.logger = logging.getLogger("everpad-editor")
        self.logger.setLevel(logging.DEBUG)
        fh = logging.FileHandler(os.path.expanduser("~/.everpad/logs/everpad.log"))
        fh.setLevel(logging.DEBUG)
        formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
        fh.setFormatter(formatter)
        self.logger.addHandler(fh)

        self.app = QApplication.instance()
        self.timer = QBasicTimer()
        self.note = note
        self.closed = False
        self.ui = Ui_Editor()
        self.ui.setupUi(self)
        self.setWindowIcon(get_icon())
        self.alternatives_template = self.ui.alternativeVersions.text()
        self.init_controls()
        self.load_note(note)
        self.update_title()
        self.mark_untouched()
        geometry = self.app.settings.value("note-geometry-%d" % self.note.id)
        if not geometry:
            geometry = self.app.settings.value("note-geometry-default")
        if geometry:
            self.restoreGeometry(geometry)
        self.resource_edit.note = note
        self.timer.start(10000, self)
Exemplo n.º 2
0
 def __init__(self, key, config):
   super(ClickTimer, self).__init__()
   self.timer = QBasicTimer()
   self.key = key
   self.keyPressed = False
   self.config = config
   self.reset()
Exemplo n.º 3
0
class GameEngine:
    """ Represents the engine for managing the game """
    Speed = 10

    def __init__(self):
        """ Instantiate the Game Engine """
        self.timer = QBasicTimer()
        
        self.window = None
        self.controller = None

        self.object = QObject()
        self.object.timerEvent = self.timerEvent

    def start(self, window, controller):
        """ Start the Game Engine """
        self.controller = controller
        self.window = window
        self.window.keyPressEvent = self.keyPressed
        self.window.keyReleaseEvent = self.keyReleased
        self.startTimer()
        
    def startTimer(self):
        """ Start the Game Timer """
        self.timer.start(GameEngine.Speed, self.object)
        
    def stop(self):
        """ Stop the game engine game timer """
        self.timer.stop()

    def timerEvent(self, event):
        """ Run the Game Timer Loop """
        self.controller.performGameCycle()

    def updateUI(self):
        """ Set the UI to update """
        self.window.update()

    def keyPressed(self, event):
        """ Called when the window has a key pressed """
        self.controller.keyPressed(event.key())

    def keyReleased(self, event):
        """ Called when the window has a key released """
        self.controller.keyReleased(event.key())
Exemplo n.º 4
0
    def __init__(self):
        """ Instantiate the Game Engine """
        self.timer = QBasicTimer()
        
        self.window = None
        self.controller = None

        self.object = QObject()
        self.object.timerEvent = self.timerEvent
Exemplo n.º 5
0
class TorchAutoShutOff(QWidget):
  def __init__(self, camera):
    super(TorchAutoShutOff, self).__init__()
    self.camera = camera
    self.timer = QBasicTimer()
  def schedule(self, time):
    self.timer.start(time, self)
  def cancel(self):
    self.timer.stop()
  def timerEvent(self, e):
    self.timer.stop()
    if self.camera.torchState == "on":
      print "auto shut-off"
      self.camera.torchOff()
Exemplo n.º 6
0
class Editor(QMainWindow):  # TODO: kill this god shit
    """Note editor"""

    def __init__(self, note, *args, **kwargs):
        QMainWindow.__init__(self, *args, **kwargs)
        # Configure logger.
        self.logger = logging.getLogger("everpad-editor")
        self.logger.setLevel(logging.DEBUG)
        fh = logging.FileHandler(os.path.expanduser("~/.everpad/logs/everpad.log"))
        fh.setLevel(logging.DEBUG)
        formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
        fh.setFormatter(formatter)
        self.logger.addHandler(fh)

        self.app = QApplication.instance()
        self.timer = QBasicTimer()
        self.note = note
        self.closed = False
        self.ui = Ui_Editor()
        self.ui.setupUi(self)
        self.setWindowIcon(get_icon())
        self.alternatives_template = self.ui.alternativeVersions.text()
        self.init_controls()
        self.load_note(note)
        self.update_title()
        self.mark_untouched()
        geometry = self.app.settings.value("note-geometry-%d" % self.note.id)
        if not geometry:
            geometry = self.app.settings.value("note-geometry-default")
        if geometry:
            self.restoreGeometry(geometry)
        self.resource_edit.note = note
        self.timer.start(10000, self)

    def timerEvent(self, event):
        if self.touched:
            self.save(notify=False)

    def init_controls(self):
        self.ui.menubar.hide()
        self.ui.resourceArea.hide()
        self.note_edit = ContentEdit(self, self.ui.contentView, self.text_changed)
        self.tag_edit = TagEdit(self, self.ui.tags, self.mark_touched)
        self.notebook_edit = NotebookEdit(self, self.ui.notebook, self.mark_touched)
        self.resource_edit = ResourceEdit(self, self.ui.resourceArea, self.ui.resourceLabel, self.mark_touched)
        self.findbar = FindBar(self)
        self.init_toolbar()
        self.init_shortcuts()
        self.init_alternatives()
        self.app.data_changed.connect(self.init_alternatives)

    def init_alternatives(self):
        try:
            conflict_items = self.app.provider.get_note_alternatives(self.note.id)
            if conflict_items:
                conflicts = map(
                    lambda item: Note.from_tuple(self.app.provider.get_note(item)), self.note.conflict_items
                )
                text = self.alternatives_template % ", ".join(
                    map(lambda note: u'<a href="%d">%s</a>' % (note.id, note.title), conflicts)
                )
                self.ui.alternativeVersions.setText(text)
                self.ui.alternativeVersions.linkActivated.connect(
                    lambda id: self.app.indicator.open(Note.from_tuple(self.app.provider.get_note(int(id))))
                )
            else:
                self.ui.alternativeVersions.hide()
        except DBusException:
            self.ui.alternativeVersions.hide()

    def init_shortcuts(self):
        self.save_btn.setShortcut(QKeySequence("Ctrl+s"))
        self.close_btn.setShortcut(QKeySequence("Ctrl+q"))
        self.email_btn.setShortcut(QKeySequence("Ctrl+Shift+e"))
        self.print_btn.setShortcut(QKeySequence(self.tr("Ctrl+p")))
        QShortcut(QKeySequence(self.tr("Ctrl+w")), self, self.save_and_close)

    def init_toolbar(self):
        self.save_btn = self.ui.toolBar.addAction(QIcon.fromTheme("document-save"), self.tr("Save"), self.save)
        self.close_btn = self.ui.toolBar.addAction(
            QIcon.fromTheme("window-close"), self.tr("Close without saving"), self.close
        )
        self.ui.toolBar.addAction(QIcon.fromTheme("edit-delete"), self.tr("Remove note"), self.delete)
        self.print_btn = self.ui.toolBar.addAction(
            QIcon.fromTheme("document-print"), self.tr("Print note"), self.note_edit.print_
        )
        self.email_btn = self.ui.toolBar.addAction(
            QIcon.fromTheme("mail-unread"), self.tr("Email note"), self.note_edit.email_note
        )
        self.email_btn = self.ui.toolBar.addAction(
            QIcon.fromTheme("emblem-shared"), self.tr("Share note"), self.share_note
        )
        self.ui.toolBar.addSeparator()
        for action in self.note_edit.get_format_actions():
            self.ui.toolBar.addAction(action)
        self.ui.toolBar.addSeparator()
        self.find_action = QAction(QIcon.fromTheme("edit-find"), self.tr("Find"), self)
        self.find_action.setCheckable(True)
        self.find_action.triggered.connect(self.findbar.toggle_visible)
        self.ui.toolBar.addAction(self.find_action)
        self.ui.toolBar.addSeparator()
        self.pin = self.ui.toolBar.addAction(
            QIcon.fromTheme("edit-pin", QIcon.fromTheme("everpad-pin")), self.tr("Pin note"), self.mark_touched
        )
        self.pin.setCheckable(True)
        self.pin.setChecked(self.note.pinnded)

    def load_note(self, note):
        self.logger.debug('Loading note: "%s"' % note.title)
        self.resource_edit.resources = map(Resource.from_tuple, self.app.provider.get_note_resources(note.id))
        self.notebook_edit.notebook = note.notebook
        self.note_edit.title = note.title
        self.logger.debug('Note content: "%s"' % note.content)
        self.note_edit.content = note.content
        self.tag_edit.tags = note.tags

    def update_note(self):
        self.logger.debug('Updating note: "%s"' % self.note_edit.title)
        self.note.notebook = self.notebook_edit.notebook
        self.note.title = self.note_edit.title
        self.note.content = self.note_edit.content
        self.note.tags = dbus.Array(self.tag_edit.tags, signature="s")
        self.note.pinnded = self.pin.isChecked()

    def closeEvent(self, event):
        event.ignore()
        self.save_and_close()

    def text_changed(self):
        self.update_title()
        self.mark_touched()

    def update_title(self):
        title = self.note_edit.title
        if self.note.conflict_parent:
            title += self.tr(" alternative of: %s") % (
                Note.from_tuple(self.app.provider.get_note(self.note.conflict_parent)).title,
            )
        self.setWindowTitle(self.tr("Everpad / %s") % title)

    @Slot()
    def save(self, notify=True):
        self.logger.debug('Saving note: "%s"' % self.note.title)
        self.mark_untouched()
        self.update_note()
        self.app.provider.update_note(self.note.struct)
        self.app.provider.update_note_resources(
            self.note.id,
            dbus.Array(map(lambda res: res.struct, self.resource_edit.resources), signature=Resource.signature),
        )
        if notify:
            self.app.send_notify(self.tr('Note "%s" saved!') % self.note.title)

    @Slot()
    def save_and_close(self):
        if self.touched:
            self.save()
        self.close()

    @Slot()
    def delete(self):
        msgBox = QMessageBox(
            QMessageBox.Critical,
            self.tr("You are trying to delete a note"),
            self.tr("Are you sure want to delete this note?"),
            QMessageBox.Yes | QMessageBox.No,
        )
        ret = msgBox.exec_()
        if ret == QMessageBox.Yes:
            self.update_note()
            self.app.provider.delete_note(self.note.id)
            self.app.send_notify(self.tr('Note "%s" deleted!') % self.note.title)
            self.close()

    @Slot()
    def close(self):
        msg = QMessageBox(
            QMessageBox.Critical,
            self.tr("Close without Saving"),
            self.tr("Are you sure want to close this note without saving?"),
            QMessageBox.Yes | QMessageBox.No,
        )
        if not self.touched or msg.exec_() == QMessageBox.Yes:
            self.hide()
            self.closed = True
            self.app.settings.setValue("note-geometry-%d" % self.note.id, self.saveGeometry())
            self.app.settings.setValue("note-geometry-default", self.saveGeometry())

    @Slot()
    def mark_touched(self):
        self.touched = True
        self.ui.actionSave.setEnabled(True)
        self.save_btn.setEnabled(True)

    def mark_untouched(self):
        self.touched = False
        self.ui.actionSave.setEnabled(False)
        self.save_btn.setEnabled(False)

    def share_note(self):
        dialog = ShareNoteDialog(self.note, parent=self)
        dialog.exec_()
Exemplo n.º 7
0
 def __init__(self, camera):
   super(TorchAutoShutOff, self).__init__()
   self.camera = camera
   self.timer = QBasicTimer()
Exemplo n.º 8
0
class ClickTimer(QWidget):
  def __init__(self, key, config):
    super(ClickTimer, self).__init__()
    self.timer = QBasicTimer()
    self.key = key
    self.keyPressed = False
    self.config = config
    self.reset()
  def reset(self):
    self.timer.stop()
    self.presses = []
    self.releases = []
    self.longClickStarted = False
  def nowMs(self):
    return monotonic_time() * 1000
  def checkEvent(self):
    now = self.nowMs()
    self.timer.stop()
    if len(self.presses) == 0:
      if self.longClickStarted and len(self.releases) > 0:
        self.click("longClickStop")
      else:
        self.reset()
    elif len(self.presses) == 1:
      press = self.presses[0]
      if len(self.releases) == 0:
        if now - press > self.config.longClickDelayMs:
          self.click("longClickStart")
          self.longClickStarted = True
        else:
          self.schedule(self.config.longClickDelayMs - (now - press))
      else:
        if now - press > self.config.doubleClickDelayMs:
          self.click("singleClick")
        else:
          self.schedule(self.config.doubleClickDelayMs - (now - press))
    elif len(self.presses) == 2:
      press = self.presses[0]
      if now - press > self.config.trebleClickDelayMs:
        self.click("doubleClick")
      else:
        self.schedule(self.config.trebleClickDelayMs - (now - press))
    else:
      self.click("trebleClick")
  def schedule(self, time):
    self.timer.start(time, self)
  def click(self, clickType):
    self.reset()
    self.config.checkConfigFile()
    print >> sys.stderr, str(self.key) + ": " + clickType

    actionMaps = self.config.getActionMapSet()
    for a in actionMaps.getActionMapsForKey(self.key, clickType):
      a.maybeRun()
  def timerEvent(self, e):
    self.timer.stop()
    self.checkEvent()
  def receivePress(self):
    self.presses.append(self.nowMs())
    self.checkEvent()
  def receiveRelease(self):
    self.releases.append(self.nowMs())
    self.checkEvent()
  def keyEvent(self, state):
    if state == STATE_ON and not self.keyPressed:
      self.keyPressed = True
      self.receivePress()
    elif state == STATE_OFF:
      self.keyPressed = False
      self.receiveRelease()