Skip to content

Instantly share code, notes, and snippets.

@keimina
Created March 14, 2016 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keimina/91b017fd1e8ec29b3967 to your computer and use it in GitHub Desktop.
Save keimina/91b017fd1e8ec29b3967 to your computer and use it in GitHub Desktop.
import os
from glob import glob
import cv2
import numpy as np
from numpy import arange
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backend_bases import key_press_handler
from matplotlib.backends.backend_qt4agg import (FigureCanvasQTAgg as FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from PySide.QtCore import *
from PySide.QtGui import *
class ImageFileIcon(QWidget):
clicked_ = Signal(QMouseEvent)
def __init__(self, parent=None, img="1.png", txt="some-text-here", size=100, description=""):
QWidget.__init__(self)
self.layout = QVBoxLayout()
self.imglabel = QLabel()
self.txtlabel = QLabel()
self.description = description
#set
self.imglabel.setPixmap(QPixmap(img).scaled(size, size))
self.txtlabel.setText(txt)
self.layout.addWidget(self.imglabel)
self.layout.addWidget(self.txtlabel)
self.setLayout(self.layout)
def mousePressEvent(self, event):
print "%s was clicked."%self.txtlabel.text()
self.clicked_.emit(event)
class LineEditButton(QWidget):
lineEditChangedSignal = Signal(str)
def __init__(self, btntxt="Open"):
QWidget.__init__(self)
self.layout = QHBoxLayout()
self.lineEdit = QLineEdit()
self.button = QPushButton()
#set
self.setAcceptDrops(True)
self.button.clicked.connect(self.myClickedFunc)
self.button.setText(btntxt)
self.layout.addWidget(self.lineEdit)
self.layout.addWidget(self.button)
self.setLayout(self.layout)
################################################################
# Implement Drag and Drop
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(Qt.CopyAction)
event.accept()
else:
event.ignore()
def dropEvent(self, event):
for url in event.mimeData().urls():
path = url.toLocalFile()
self.changeLineEditText(path)
# Implement Drag and Drop
################################################################
def myClickedFunc(self):
fileName, _ = QFileDialog.getOpenFileName(self,
"Open Image",
"./",
"Image Files (*.png *.bmp *.jpg *.tiff)")
self.changeLineEditText(fileName)
def changeLineEditText(self, text):
self.lineEdit.setText(text)
path_unicode = self.lineEdit.text()
self.lineEditChangedSignal.emit(path_unicode)
class MplView(QWidget):
def __init__(self):
QWidget.__init__(self)
self.layout = QVBoxLayout()
self.fig = Figure()
self.axes = self.fig.add_subplot(111)
self.lines, = self.axes.plot([], [])
self.canvas = FigureCanvas(self.fig)
self.mpl_toolbar = NavigationToolbar(self.canvas, self)
#set
#self.lines.set_linestyle("None")
print self
self.canvas.setParent(self)
self.canvas.setFocusPolicy(Qt.StrongFocus)
self.canvas.setFocus()
self.canvas.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.layout.addWidget(self.mpl_toolbar)
self.layout.addWidget(self.canvas)
self.setLayout(self.layout)
def showImage(self, path):
self.im = cv2.imread(path, 0)
self.axes.imshow(self.im, cmap="gray", interpolation='nearest')
self.updateMplView()
def updateMplView(self):
self.canvas.draw_idle()
class MainPane(QWidget):
def __init__(self):
QWidget.__init__(self)
self.layout = QVBoxLayout()
self.lineEditButton = LineEditButton()
self.mplView = MplView()
#set
self.layout.addWidget(self.lineEditButton)
self.layout.addWidget(self.mplView)
self.setLayout(self.layout)
#connect
self.lineEditButton.lineEditChangedSignal.connect(self.mplView.showImage)
try:
app = QApplication([])
except:
pass
mainPane = MainPane()
mainPane.show()
mainPane.raise_()
try:
app.exec_()
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment