historypart.cpp
Go to the documentation of this file.00001 #include "historypart.h"
00002
00003 #include <kaction.h>
00004 #include <kdebug.h>
00005 #include <kdevgenericfactory.h>
00006 #include <kmainwindow.h>
00007 #include <kparts/part.h>
00008 #include <kpopupmenu.h>
00009 #include <kiconloader.h>
00010
00011 #include <ktexteditor/editor.h>
00012
00013 #include "kdevcore.h"
00014 #include "kdevpartcontroller.h"
00015 #include "kdevmainwindow.h"
00016
00017
00018 class HistoryEntry
00019 {
00020 public:
00021 HistoryEntry(KParts::Part *part) : m_part(part) {};
00022
00023 KParts::Part *m_part;
00024 KURL m_url;
00025
00026 };
00027
00028 static const KAboutData data("kdevhistory", I18N_NOOP("History"), "1.0");
00029
00030 typedef KDevGenericFactory<HistoryPart> HistoryPartFactory;
00031
00032 K_EXPORT_COMPONENT_FACTORY(libkdevhistory, HistoryPartFactory(&data))
00033
00034 HistoryPart::HistoryPart(QObject *parent, const char *name, const QStringList &)
00035 : KDevPlugin("History", "history", parent, name ? name : "HistoryPart"), m_restoring(false)
00036 {
00037 setInstance(HistoryPartFactory::instance());
00038
00039 setXMLFile("kdevhistory.rc");
00040
00041 connect(partController(), SIGNAL(partRemoved(KParts::Part*)), this, SLOT(partRemoved(KParts::Part*)));
00042 connect(partController(), SIGNAL(activePartChanged(KParts::Part*)), this, SLOT(activePartChanged(KParts::Part*)));
00043 setupActions();
00044
00045 m_history.setAutoDelete(true);
00046
00047 updateActions();
00048 }
00049
00050
00051 HistoryPart::~HistoryPart()
00052 {
00053 }
00054
00055
00056 void HistoryPart::setupActions()
00057 {
00058 m_backAction = new KToolBarPopupAction(i18n("&Back"), "back", 0,
00059 this, SLOT(backActivated()),
00060 actionCollection(), "history_back");
00061 m_backAction->setToolTip(i18n("Back"));
00062 m_backAction->setWhatsThis(i18n("<b>Back</b><p>Moves backwards one step in the browsing history."));
00063
00064 m_forwardAction = new KToolBarPopupAction(i18n("&Forward"), "forward", 0,
00065 this, SLOT(forwardActivated()),
00066 actionCollection(), "history_forward");
00067 m_forwardAction->setToolTip(i18n("Forward"));
00068 m_forwardAction->setWhatsThis(i18n("<b>Forward</b><p>Moves forward one step in the browsing history."));
00069
00070 connect(m_backAction->popupMenu(), SIGNAL(aboutToShow()),
00071 this, SLOT(backAboutToShow()));
00072 connect(m_backAction->popupMenu(), SIGNAL(activated(int)),
00073 this, SLOT(backPopupActivated(int)));
00074 connect(m_forwardAction->popupMenu(), SIGNAL(aboutToShow()),
00075 this, SLOT(forwardAboutToShow()));
00076 connect(m_forwardAction->popupMenu(), SIGNAL(activated(int)),
00077 this, SLOT(forwardPopupActivated(int)));
00078 }
00079
00080
00081 void HistoryPart::backAboutToShow()
00082 {
00083 KPopupMenu *popup = m_backAction->popupMenu();
00084 popup->clear();
00085
00086 int savePos = m_history.at();
00087 for (int i=0; i<10 && m_history.prev(); ++i)
00088 popup->insertItem(m_history.current()->m_url.fileName() );
00089
00090 m_history.at(savePos);
00091 }
00092
00093
00094 void HistoryPart::forwardAboutToShow()
00095 {
00096 KPopupMenu *popup = m_forwardAction->popupMenu();
00097 popup->clear();
00098
00099 int savePos = m_history.at();
00100 for (int i=0; i<10 && m_history.next(); ++i)
00101 popup->insertItem(m_history.current()->m_url.fileName() );
00102
00103 m_history.at(savePos);
00104 }
00105
00106
00107 void HistoryPart::backPopupActivated(int id)
00108 {
00109 int by = m_backAction->popupMenu()->indexOf(id);
00110
00111 saveState(partController()->activePart());
00112 for (int i=0; i < by; ++i)
00113 m_history.prev();
00114 if(m_history.prev()==0L) m_history.first();
00115
00116 restoreState();
00117
00118 updateActions();
00119 }
00120
00121
00122 void HistoryPart::forwardPopupActivated(int id)
00123 {
00124 int by = m_forwardAction->popupMenu()->indexOf(id)+1;
00125
00126 saveState(partController()->activePart());
00127 for (int i=0; i < by; ++i)
00128 m_history.next();
00129 if(m_history.current()==0L) m_history.last();
00130
00131 restoreState();
00132
00133 updateActions();
00134 }
00135
00136
00137 void HistoryPart::updateActions()
00138 {
00139 m_backAction->setEnabled(m_history.current() != m_history.getFirst());
00140 m_forwardAction->setEnabled(m_history.current() != m_history.getLast());
00141
00142 kdDebug(9031) << "history-length=" << m_history.count() << endl;
00143 }
00144
00145
00146 void HistoryPart::addHistoryEntry(HistoryEntry *entry)
00147 {
00148 HistoryEntry *current = m_history.current();
00149 while (m_history.getLast() != current)
00150 m_history.removeLast();
00151 m_history.append(entry);
00152 m_history.last();
00153
00154 saveState(entry->m_part);
00155
00156 updateActions();
00157 }
00158
00159 void HistoryPart::saveState(KParts::Part *part)
00160 {
00161 if (!part || !part->inherits( "KTextEditor::Editor" ) )
00162 return;
00163
00164 HistoryEntry *entry = m_history.current();
00165 if (!entry)
00166 return;
00167
00168 entry->m_part = part;
00169
00170 KParts::ReadOnlyPart *ro_part = dynamic_cast<KParts::ReadOnlyPart*>(part);
00171 if (ro_part)
00172 entry->m_url = ro_part->url();
00173 else
00174 entry->m_url = KURL();
00175 }
00176
00177
00178 void HistoryPart::restoreState()
00179 {
00180 HistoryEntry *entry = m_history.current();
00181 if (!entry)
00182 return;
00183
00184 m_restoring = true;
00185
00186 if ( partController()->parts()->contains( entry->m_part ) )
00187 {
00188 partController()->setActivePart(entry->m_part);
00189 if (entry->m_part->widget())
00190 {
00191 mainWindow()->raiseView(entry->m_part->widget());
00192 entry->m_part->widget()->setFocus();
00193 }
00194 }
00195 else
00196 {
00197 kdDebug( 9031 ) << "Found invalid part in history - NOT restoring!" << endl;
00198 }
00199
00200 m_restoring = false;
00201
00202 updateActions();
00203 }
00204
00205
00206 void HistoryPart::backActivated()
00207 {
00208 saveState(partController()->activePart());
00209
00210 if(m_history.prev()==0L) m_history.first();
00211
00212 restoreState();
00213 }
00214
00215
00216 void HistoryPart::forwardActivated()
00217 {
00218 saveState(partController()->activePart());
00219
00220 if(m_history.next()==0L) m_history.last();
00221
00222 restoreState();
00223 }
00224
00225
00226 void HistoryPart::activePartChanged(KParts::Part *part)
00227 {
00228 kdDebug(9031) << "HistoryPart::activePartChanged()" << endl;
00229
00230 if (!part || m_restoring || !part->inherits( "KTextEditor::Editor" ) )
00231 return;
00232
00233 HistoryEntry *entry = new HistoryEntry(part);
00234
00235 addHistoryEntry(entry);
00236 }
00237
00238
00239 void HistoryPart::partRemoved(KParts::Part *part)
00240 {
00241 QPtrListIterator<HistoryEntry> it(m_history);
00242
00243 for ( ; it.current(); ++it)
00244 if (it.current()->m_part == part)
00245 m_history.remove(it.current());
00246
00247 updateActions();
00248 }
00249
00250
00251 #include "historypart.moc"
This file is part of the documentation for KDevelop Version 3.1.2.