initial kgraphinterface, by Yannick Schpilka
[kgraphinterface.git] / src / kgraphinterface_part.cpp
blobc9bb828796a26147fefd893ed8ec5bfd5fe0e3f4
2 #include "kgraphinterface_part.h"
4 #include <kinstance.h>
5 #include <kaction.h>
6 #include <kstdaction.h>
7 #include <kfiledialog.h>
8 #include <kglobal.h>
9 #include <klocale.h>
11 #include <qfile.h>
12 #include <qtextstream.h>
13 #include <qxml.h>
14 #include "graph.h"
15 #include "gihandler.h"
17 KGraphInterfacePart::KGraphInterfacePart( QWidget *parentWidget, const char *widgetName,
18 QObject *parent, const char *name )
19 : KParts::ReadWritePart(parent, name)
21 // we need an instance
22 setInstance( KGraphInterfacePartFactory::instance() );
24 // this should be your custom internal widget
25 m_widget = new Graph( parentWidget, "Graphe" );
27 // notify the part that this is our internal widget
28 setWidget(m_widget);
30 // create our actions
31 KStdAction::open(this, SLOT(fileOpen()), actionCollection());
32 KStdAction::saveAs(this, SLOT(fileSaveAs()), actionCollection());
33 KStdAction::save(this, SLOT(save()), actionCollection());
35 // set our XML-UI resource file
36 setXMLFile("kgraphinterface_part.rc");
38 // we are read-write by default
39 setReadWrite(true);
41 // we are not modified since we haven't done anything yet
42 setModified(false);
45 KGraphInterfacePart::~KGraphInterfacePart()
49 void KGraphInterfacePart::setReadWrite(bool rw)
51 // notify your internal widget of the read-write state
52 /* m_widget->setReadOnly(!rw);
53 if (rw)
54 connect(m_widget, SIGNAL(textChanged()),
55 this, SLOT(setModified()));
56 else
58 disconnect(m_widget, SIGNAL(textChanged()),
59 this, SLOT(setModified()));
62 ReadWritePart::setReadWrite(rw);
65 void KGraphInterfacePart::setModified(bool modified)
67 // get a handle on our Save action and make sure it is valid
68 KAction *save = actionCollection()->action(KStdAction::stdName(KStdAction::Save));
69 if (!save)
70 return;
72 // if so, we either enable or disable it based on the current
73 // state
74 if (modified)
75 save->setEnabled(true);
76 else
77 save->setEnabled(false);
79 // in any event, we want our parent to do it's thing
80 ReadWritePart::setModified(modified);
83 bool KGraphInterfacePart::openFile()
85 // m_file is always local so we can use QFile on it
86 QFile file(m_file);
87 if (file.open(IO_ReadOnly) == false)
88 return false;
90 //On parse le fichier recu
91 GIHandler handler (m_widget);
92 QXmlSimpleReader reader;
93 reader.setContentHandler(&handler);
94 reader.setErrorHandler(&handler);
95 QXmlInputSource source(&file);
96 reader.parse(source);
98 file.close();
100 // now that we have the entire file, display it
101 // m_widget->setText(str);
103 // just for fun, set the status bar
104 emit setStatusBarText( m_url.prettyURL() );
106 return true;
109 bool KGraphInterfacePart::saveFile()
111 // if we aren't read-write, return immediately
112 if (isReadWrite() == false)
113 return false;
115 // m_file is always local, so we use QFile
116 QFile file(m_file);
117 if (file.open(IO_WriteOnly) == false)
118 return false;
120 // use QTextStream to dump the text to the file
121 QTextStream stream(&file);
122 stream << m_widget->toXml();
124 file.close();
126 return true;
129 void KGraphInterfacePart::fileOpen()
131 // this slot is called whenever the File->Open menu is selected,
132 // the Open shortcut is pressed (usually CTRL+O) or the Open toolbar
133 // button is clicked
134 QString file_name = KFileDialog::getOpenFileName();
136 if (file_name.isEmpty() == false)
137 openURL(file_name);
140 void KGraphInterfacePart::fileSaveAs()
142 // this slot is called whenever the File->Save As menu is selected,
143 QString file_name = KFileDialog::getSaveFileName();
144 if (file_name.isEmpty() == false)
145 saveAs(file_name);
149 // It's usually safe to leave the factory code alone.. with the
150 // notable exception of the KAboutData data
151 #include <kaboutdata.h>
152 #include <klocale.h>
154 KInstance* KGraphInterfacePartFactory::s_instance = 0L;
155 KAboutData* KGraphInterfacePartFactory::s_about = 0L;
157 KGraphInterfacePartFactory::KGraphInterfacePartFactory()
158 : KParts::Factory()
162 KGraphInterfacePartFactory::~KGraphInterfacePartFactory()
164 delete s_instance;
165 delete s_about;
167 s_instance = 0L;
170 KParts::Part* KGraphInterfacePartFactory::createPartObject( QWidget *parentWidget, const char *widgetName,
171 QObject *parent, const char *name,
172 const char *classname, const QStringList &args )
174 // Create an instance of our Part
175 KGraphInterfacePart* obj = new KGraphInterfacePart( parentWidget, widgetName, parent, name );
177 // See if we are to be read-write or not
178 if (QCString(classname) == "KParts::ReadOnlyPart")
179 obj->setReadWrite(false);
181 return obj;
184 KInstance* KGraphInterfacePartFactory::instance()
186 if( !s_instance )
188 s_about = new KAboutData("kgraphinterfacepart", I18N_NOOP("KGraphInterfacePart"), "0.1");
189 s_about->addAuthor("Schpilka Yannick", 0, "schpilka@gmail.com");
190 s_instance = new KInstance(s_about);
192 return s_instance;
195 extern "C"
197 void* init_libkgraphinterfacepart()
199 KGlobal::locale()->insertCatalogue("kgraphinterface");
200 return new KGraphInterfacePartFactory;
204 #include "kgraphinterface_part.moc"