fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kparts / tests / notepad.cpp
blobb6c2d9f120786c37a0b5904b74e868f5ed04243e
2 #include "notepad.h"
3 #include <kparts/partmanager.h>
4 #include <kparts/mainwindow.h>
6 #include <QtGui/QSplitter>
7 #include <QtCore/QFile>
8 #include <QtCore/QTextStream>
9 #include <QtGui/QTextEdit>
11 #include <kaboutdata.h>
12 #include <kapplication.h>
13 #include <kdebug.h>
14 #include <kaction.h>
15 #include <kactioncollection.h>
16 #include <klocale.h>
17 #include <kstatusbar.h>
18 #include <kstandarddirs.h>
19 #include <kpluginfactory.h>
21 K_PLUGIN_FACTORY(NotepadFactory, registerPlugin<NotepadPart>();)
22 K_EXPORT_PLUGIN(NotepadFactory("notepadpart"))
24 NotepadPart::NotepadPart( QWidget* parentWidget,
25 QObject* parent,
26 const QVariantList& )
27 : KParts::ReadWritePart( parent )
29 setComponentData(NotepadFactory::componentData(), false);
31 m_edit = new QTextEdit( parentWidget );
32 m_edit->setPlainText( "NotepadPart's multiline edit" );
33 setWidget( m_edit );
35 KAction* searchReplace = new KAction( "Search and replace", this );
36 actionCollection()->addAction( "searchreplace", searchReplace );
37 connect(searchReplace, SIGNAL(triggered()), this, SLOT(slotSearchReplace()));
39 // KXMLGUIClient looks in the "data" resource for the .rc files
40 // This line is for test programs only!
41 componentData().dirs()->addResourceDir( "data", KDESRCDIR );
42 setXMLFile( "notepadpart.rc" );
44 setReadWrite( true );
46 // Always write this as the last line of your constructor
47 loadPlugins();
50 NotepadPart::~NotepadPart()
54 void NotepadPart::setReadWrite( bool rw )
56 m_edit->setReadOnly( !rw );
57 if (rw)
58 connect( m_edit, SIGNAL( textChanged() ), this, SLOT( setModified() ) );
59 else
60 disconnect( m_edit, SIGNAL( textChanged() ), this, SLOT( setModified() ) );
62 ReadWritePart::setReadWrite( rw );
65 KAboutData* NotepadPart::createAboutData()
67 return new KAboutData( "notepadpart", 0, ki18n( "Notepad" ), "2.0" );
70 bool NotepadPart::openFile()
72 kDebug() << "NotepadPart: opening " << localFilePath();
73 QFile f(localFilePath());
74 QString s;
75 if ( f.open(QIODevice::ReadOnly) ) {
76 QTextStream t( &f );
77 t.setCodec( "UTF-8" );
78 s = t.readAll();
79 f.close();
81 m_edit->setPlainText(s);
83 emit setStatusBarText( url().prettyUrl() );
85 return true;
88 bool NotepadPart::saveFile()
90 if ( !isReadWrite() )
91 return false;
92 QFile f(localFilePath());
93 QString s;
94 if ( f.open(QIODevice::WriteOnly) ) {
95 QTextStream t( &f );
96 t.setCodec( "UTF-8" );
97 t << m_edit->toPlainText();
98 f.close();
99 return true;
100 } else
101 return false;
104 void NotepadPart::slotSearchReplace()
106 // What's this ? (David)
108 QValueList<KParts::XMLGUIServant *> plugins = KParts::Plugin::pluginServants( this );
109 QValueList<KParts::XMLGUIServant *>::ConstIterator it = plugins.begin();
110 QValueList<KParts::XMLGUIServant *>::ConstIterator end = plugins.end();
111 for (; it != end; ++it )
112 factory()->removeServant( *it );
116 #include "notepad.moc"