Hola, ayer comencé a hacer un HTML Helper, para quienes no manejen
html, el programa es básico, para la próxima versión pretendo añadirle
otras cosas, CSS entre ellas, las actuales funciones :
- Añadir Textarea
- Añadir Input
- Añadir Imagen
- Añadir Link
- Añadir Boton
- Cambiar Fondo(despliega paleta de colores)
Tambien trae aparte la paleta de colores & el dialogo para buscar el
nombre de alguna Font.
El programa realiza un preview al dar clic en el boton "Preview"
Entre las opciones del menú "Archivo" :
- Abrir desde un .html
- Abrir desde una URL(ingresamos url y traemos el código al editor)
- Guardar el html que generamos
- Salir
Al lado de las pestañas Código & Prevista tenemos las opciones
Bold , Underline , Italic.
Un screenshot : (http://anycode.s.gp/misubidas/editor_html_final.png)
Lo que más importa, el código :
# -*- coding: utf-8 -*-
import sys,re,urllib2
from PyQt4 import QtCore, QtGui, QtWebKit
class create(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
#Boton Bold
self.bold = QtGui.QPushButton("Bold",self)
self.bold.setGeometry(172,26,41,25)
self.connect(self.bold,QtCore.SIGNAL("clicked()"), self.make_bold)
#Boton Italic
self.italic = QtGui.QPushButton("Italic",self)
self.italic.setGeometry(216,26,41,25)
self.connect(self.italic,QtCore.SIGNAL("clicked()"), self.make_italic)
#Boton Underline
self.underline = QtGui.QPushButton("Underline",self)
self.underline.setGeometry(261,26,63,25)
self.connect(self.underline,QtCore.SIGNAL("clicked()"), self.make_underline)
#Nuevo menu "Archivo"
menu_archivo = self.menuBar()
archivo = menu_archivo.addMenu('&Archivo')
#Menu Abrir
abrir = QtGui.QAction('&Abrir',self)
abrir.setShortcut('Ctrl+O')
archivo.addAction(abrir)
self.connect(abrir, QtCore.SIGNAL('triggered()'),self.abrir)
#Menu Abrir URL
abrir_url = QtGui.QAction('Abrir desde &URL',self)
abrir_url.setShortcut('Ctrl+U')
archivo.addAction(abrir_url)
self.connect(abrir_url, QtCore.SIGNAL('triggered()'),self.get_source)
#Menu Guardar
guardar = QtGui.QAction('&Guardar',self)
guardar.setShortcut('Ctrl+S')
archivo.addAction(guardar)
self.connect(guardar, QtCore.SIGNAL('triggered()'),self.guardar)
#Menu salir
salida = QtGui.QAction('&Exit', self)
salida.setShortcut('Ctrl+W')
archivo.addAction(salida)
self.connect(salida, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
#Fin del menú Archivo
#Nuevo menú Herramientas
menu_inputs = self.menuBar()
herramientas = menu_inputs.addMenu('&Herramientas')
#Menu textarea
textarea = QtGui.QAction('Agregar &TextArea', self)
herramientas.addAction(textarea)
self.connect(textarea, QtCore.SIGNAL('triggered()'), self.add_textarea)
#Menu input
inputx = QtGui.QAction('Agregar &Input', self)
herramientas.addAction(inputx)
self.connect(inputx, QtCore.SIGNAL('triggered()'), self.add_input)
#Menu Boton
boton = QtGui.QAction('Agregar &Boton', self)
herramientas.addAction(boton)
self.connect(boton, QtCore.SIGNAL('triggered()'), self.add_button)
#Menu Imagen
imagen = QtGui.QAction('Agregar I&magen', self)
herramientas.addAction(imagen)
self.connect(imagen, QtCore.SIGNAL('triggered()'), self.add_imagen)
#Menu Cambiar Fondo
fondo = QtGui.QAction('Color de &Fondo', self)
herramientas.addAction(fondo)
self.connect(fondo, QtCore.SIGNAL('triggered()'), self.add_fondo)
#Menu Link
link = QtGui.QAction('Agregar &Link', self)
herramientas.addAction(link)
self.connect(link, QtCore.SIGNAL('triggered()'), self.add_link)
#Fin menú Herramientas
self.resize(729, 674)
self.pest_code = QtGui.QTabWidget(self)
self.pest_code.setGeometry(QtCore.QRect(0, 30, 711, 631))
self.tab = QtGui.QWidget()
self.html_plano = QtGui.QPlainTextEdit(self.tab)
self.html_plano.setGeometry(QtCore.QRect(10, 30, 691, 571))
self.boton_previw = QtGui.QPushButton("Preview",self.tab)
self.boton_previw.setGeometry(QtCore.QRect(10, 4,83, 21))
self.pest_code.addTab(self.tab, "")
self.tab_2 = QtGui.QWidget()
#Nuevo menú Fuente / Colores
font_color = self.menuBar()
fuentes_colores = font_color.addMenu('&Fuente / Colores')
#Menu Buscar Color
search_color = QtGui.QAction('Buscar Color', self)
fuentes_colores.addAction(search_color)
self.connect(search_color, QtCore.SIGNAL('triggered()'), self.find_color)
#Menu buscar Fuente
search_font = QtGui.QAction('Buscar Fuente', self)
fuentes_colores.addAction(search_font)
self.connect(search_font, QtCore.SIGNAL('triggered()'), self.find_font)
self.vista_web = QtWebKit.QWebView(self.tab_2)
self.vista_web.setGeometry(QtCore.QRect(10, 10, 691, 591))
self.pest_code.addTab(self.tab_2, "")
self.pest_code.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(self)
self.setWindowTitle("Create HTML Helper.")
self.pest_code.setTabText(self.pest_code.indexOf(self.tab),"Codigo")
self.pest_code.setTabText(self.pest_code.indexOf(self.tab_2), "Prevista")
self.connect(self.boton_previw, QtCore.SIGNAL('clicked()'), self.done)
def make_bold(self):
self.html_final = "%s<br />\n<b></b>"%self.html_plano.toPlainText()
self.html_plano.setPlainText(self.html_final)
def make_italic(self):
self.html_final = "%s<br />\n<i></i>"%self.html_plano.toPlainText()
self.html_plano.setPlainText(self.html_final)
def make_underline(self):
self.html_final = "%s<br />\n<u></u>"%self.html_plano.toPlainText()
self.html_plano.setPlainText(self.html_final)
def done(self):
self.salida = self.html_plano.toPlainText()
temporal = open("tmp.html","w")
temporal.write(self.salida)
temporal.close()
self.vista_web.setUrl(QtCore.QUrl("tmp.html"))
self.pest_code.setCurrentIndex(1)
def guardar(self):
self.obtener_html = self.html_plano.toPlainText()
try:
nombre_save = QtGui.QFileDialog.getSaveFileName(self, 'Guardar Archivo','/home')
guardando_html = open(nombre_save, "w")
guardando_html.write(self.obtener_html)
guardando_html.close()
alertas.show()
alertas.alerta("Advertencia","Guardado!")
except:
alertas.show()
alertas.alerta("Advertencia","No Guardado")
def abrir(self):
nombre_open = QtGui.QFileDialog.getOpenFileName(self, 'Abrir Archivo','/home')
abriendo_html = open(nombre_open, "r")
contenido = abriendo_html.read()
self.html_plano.setPlainText(contenido)
def add_textarea(self):
rows, respuesta_rows = QtGui.QInputDialog.getText(self, 'Rows','Numero de Lineas:')
if respuesta_rows:
rows = " rows=\"%s\""%str(rows)
else:
rows = ""
cols, respuesta_cols = QtGui.QInputDialog.getText(self, 'Cols','Numero de Columnas:')
if respuesta_cols:
cols = " cols=\"%s\""%str(cols)
else:
cols = ""
self.html_final = "%s<br />\n<textarea%s%s></textarea>"%(self.html_plano.toPlainText(),rows,cols)
self.html_plano.setPlainText(self.html_final)
def add_input(self):
value, respuesta_value = QtGui.QInputDialog.getText(self, 'Valor','Valor por defecto:')
if respuesta_value:
value = " value=\"%s\""%str(value)
else:
value = ""
self.html_final = "%s<br />\n<input%s>"%(self.html_plano.toPlainText(),value)
self.html_plano.setPlainText(self.html_final)
def add_button(self):
button, respuesta_boton = QtGui.QInputDialog.getText(self, 'Valor','Valor del Boton:')
if respuesta_boton:
button = " value=\"%s\""%str(button)
else:
button = ""
self.html_final = "%s<br />\n<input type=\"Submit\"%s>"%(self.html_plano.toPlainText(),button)
self.html_plano.setPlainText(self.html_final)
def add_imagen(self):
imagen, respuesta_imagen = QtGui.QInputDialog.getText(self, 'Valor','URL de la Imagen:')
if respuesta_imagen:
imagen = " src=\"%s\""%str(imagen)
else:
imagen = ""
width, respuesta_width = QtGui.QInputDialog.getText(self, 'Valor','Ancho:')
if respuesta_width:
width = " width=\"%s\""%str(width)
else:
width = ""
height, respuesta_height = QtGui.QInputDialog.getText(self, 'Valor','Alto:')
if respuesta_height:
height = " height=\"%s\""%str(height)
else:
height = ""
self.html_final = "%s<br />\n<img%s%s%s>"%(self.html_plano.toPlainText(),imagen,width,height)
self.html_plano.setPlainText(self.html_final)
def add_fondo(self):
color = QtGui.QColorDialog.getColor()
if color.isValid():
if not "<body" in self.html_plano.toPlainText():
self.html_final = "<body bgcolor=\"%s\">%s</body>"%(color.name(),self.html_plano.toPlainText())
self.html_plano.setPlainText(self.html_final)
else:
for i in re.findall("<body bgcolor=\"(.*)\">",self.html_plano.toPlainText()):
anterior=i
self.html_final = self.html_plano.toPlainText().replace(anterior,color.name())
self.html_plano.setPlainText(self.html_final)
else:
self.html_final = "<body bgcolor=\"#FFFFFF\">%s</body>"%(self.html_plano.toPlainText())
self.html_plano.setPlainText(self.html_final)
def find_color(self):
busca_color= QtGui.QColorDialog.getColor()
if busca_color.isValid():
alertas.show()
alertas.alerta("Advertencia","Color %s"%busca_color.name())
def get_source(self):
url, respuesta_url = QtGui.QInputDialog.getText(self, 'Get','URL:')
try:
self.html_plano.setPlainText(urllib2.urlopen(str(url)).read())
except:
alertas.show()
alertas.alerta("Advertencia","Contenido no encontrado")
def add_link(self):
link, respuesta_link = QtGui.QInputDialog.getText(self, 'Link','Link:')
if respuesta_link:
link = " href=\"%s\""%link
else:
link = ""
name, respuesta_name = QtGui.QInputDialog.getText(self, 'Nombre','Nombre:')
if respuesta_name:
name = "%s"%name
else:
name = "%s"%link
self.html_final = "%s<br />\n<a%s>%s</a>"%(self.html_plano.toPlainText(),link,name)
self.html_plano.setPlainText(self.html_final)
def find_font(self):
QtGui.QFontDialog.getFont()
class myalert(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
#self.resize(130,50)
self.setGeometry(400,300,250,50)
self.label_problema = QtGui.QLabel(self)
self.label_problema.setGeometry(40,17,180,20)
def alerta(self,error,razon):
self.setWindowTitle(error)
self.label_problema.setText(razon)
tool = QtGui.QApplication(sys.argv)
dialogo = create()
dialogo.show()
alertas = myalert()
alertas.hide()
tool.exec_()
Saludos !